线程创建的五种方法

创建线程的方法

  • 继承Thread类
public class MyThread extends Thread {

    @Override
    public void run() {
        System.out.println("MyThread");
    }

    public static void main(String[] args) {
        new MyThread().start();
    }

}
  • 实现Runable接口
public class MyThread implements Runnable {

    @Override
    public void run() {
        System.out.println("MyThread");
    }

    public static void main(String[] args) {
        new Thread(new MyThread()).start();
    }
}

继承Thread和实现Runable哪种方式比较好?实现Runable的方式比较灵活,因为java是单继承,实现Runable接口以后,该线程类还能从其他的类继承。

  • 实现Callable接口
public class MyThread implements Callable<String> {

    @Override
    public String call() {
       return "success";
    }

    public static void main(String[] args) throws Exception {
        //通过线程池启动
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<String> future = executorService.submit(new MyThread());
        //阻塞等待线程执行完
        String result = future.get();
        System.out.println(result);
        //通过FutureTask来启动
        FutureTask<String> task = new FutureTask<>(new MyThread());
        new Thread(task).start();
        result = task.get();
        System.out.println(result);
    }
}

Future用来接收线程未来可能返回值的类, Future#get() 是个阻塞方法。FutureTask可以通过new Thread运行,也能接收Callable的返回值,它的本质是继承了Runnable接口和Futrere接口

  • 通过lamda表达式创建
    public static void main(String[] args) throws Exception {
       new Thread(()->{
           System.out.println("MyThread");
       }).start();
    }
  • 使用线程池创建
    public static void main(String[] args) throws Exception {
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(()->{
            System.out.println("hello ExecutorService");
        });
    }

这五种创建线程的方式本质都是使用的new Thread()对象,然后执行start()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值