多线程_创建线程

创建线程的三种方式:继承Thread类、实现Runnable接口、实现Callable接口

继承Thread类:

public class Test {
    public static void main(String[] args) {
        TestThread testThread01 = new TestThread("testThread01");
        TestThread testThread02 = new TestThread("testThread02");
        TestThread testThread03 = new TestThread("testThread03");

        testThread01.start();
        testThread02.start();
        testThread03.start();
    }
}

class TestThread extends Thread{
    String currentThreadName;

    public TestThread(String currentThreadName) {
        this.currentThreadName = currentThreadName;
    }
    
    @Override
    public void run() {
        System.out.println("currentThread: " + currentThreadName);
    }
}

 

实现Runnable接口:

public class Test {
    public static void main(String[] args) {
        TestThread testThread = new TestThread();

        Thread thread01 = new Thread(testThread, "Thread01");
        Thread thread02 = new Thread(testThread, "Thread02");
        Thread thread03 = new Thread(testThread, "Thread03");

        thread01.start();
        thread02.start();
        thread03.start();
    }
}

class TestThread implements Runnable{

    @Override
    public void run() {
        System.out.println("currentThread: " + Thread.currentThread().getName());
    }
}

 

实现Callable接口

Callable的优势:
        01.可以自定义返回值
        02.可以抛出异常
public class Test {
    public static void main(String[] args) {
        TestThread testThread01 = new TestThread("testThread01");
        TestThread testThread02 = new TestThread("testThread02");
        TestThread testThread03 = new TestThread("testThread03");

        //创建执行服务(线程池)
        ExecutorService service = Executors.newFixedThreadPool(10);

        //提交执行 并通过Future<Boolean>接收返回值
        Future<Boolean> result01 = service.submit(testThread01);
        Future<Boolean> result02 = service.submit(testThread02);
        Future<Boolean> result03 = service.submit(testThread03);
        //获取返回值
        try {
            boolean getReturn01 = result01.get();
            boolean getReturn02 = result02.get();
            boolean getReturn03 = result03.get();

            //输出返回值
            System.out.println(getReturn01);
            System.out.println(getReturn02);
            System.out.println(getReturn03);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } finally {
            //关闭服务
            service.shutdown();
        }
    }
}

class TestThread implements Callable<Boolean> {

    String currentThreadName;

    public TestThread(String currentThreadName) {
        this.currentThreadName = currentThreadName;
    }

    @Override
    public Boolean call() throws Exception {
        System.out.println(currentThreadName);
        return true;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值