死磕 java线程系列之创建线程的8种方式

以你之姓@ 2023-06-04 04:56 170阅读 0赞

(手机横屏看源码更方便)

#

问题

(1)创建线程有哪几种方式?

(2)它们分别有什么运用场景?

简介

创建线程,是多线程编程中最基本的操作,彤哥总结了一下,大概有8种创建线程的方式,你知道吗?

继承Thread类并重写run()方法

  1. public class CreatingThread01 extends Thread {
  2. @Override
  3. public void run() {
  4. System.out.println(getName() " is running");
  5. }
  6. public static void main(String[] args) {
  7. new CreatingThread01().start();
  8. new CreatingThread01().start();
  9. new CreatingThread01().start();
  10. new CreatingThread01().start();
  11. }
  12. }

继承Thread类并重写run()方法,这种方式的弊端是一个类只能继承一个父类,如果这个类本身已经继承了其它类,就不能使用这种方式了。

实现Runnable接口

  1. public class CreatingThread02 implements Runnable {
  2. @Override
  3. public void run() {
  4. System.out.println(Thread.currentThread().getName() " is running");
  5. }
  6. public static void main(String[] args) {
  7. new Thread(new CreatingThread02()).start();
  8. new Thread(new CreatingThread02()).start();
  9. new Thread(new CreatingThread02()).start();
  10. new Thread(new CreatingThread02()).start();
  11. }
  12. }

实现Runnable接口,这种方式的好处是一个类可以实现多个接口,不影响其继承体系。

匿名内部类

  1. public class CreatingThread03 {
  2. public static void main(String[] args) {
  3. // Thread匿名类,重写Thread的run()方法
  4. new Thread() {
  5. @Override
  6. public void run() {
  7. System.out.println(getName() " is running");
  8. }
  9. }.start();
  10. // Runnable匿名类,实现其run()方法
  11. new Thread(new Runnable() {
  12. @Override
  13. public void run() {
  14. System.out.println(Thread.currentThread().getName() " is running");
  15. }
  16. }).start();
  17. // 同上,使用lambda表达式函数式编程
  18. new Thread(()->{
  19. System.out.println(Thread.currentThread().getName() " is running");
  20. }).start();
  21. }
  22. }

使用匿名类的方式,一是重写Thread的run()方法,二是传入Runnable的匿名类,三是使用lambda方式,现在一般使用第三种(java8 ),简单快捷。

实现Callabe接口

  1. public class CreatingThread04 implements Callable<Long> {
  2. @Override
  3. public Long call() throws Exception {
  4. Thread.sleep(2000);
  5. System.out.println(Thread.currentThread().getId() " is running");
  6. return Thread.currentThread().getId();
  7. }
  8. public static void main(String[] args) throws ExecutionException, InterruptedException {
  9. FutureTask<Long> task = new FutureTask<>(new CreatingThread04());
  10. new Thread(task).start();
  11. System.out.println("等待完成任务");
  12. Long result = task.get();
  13. System.out.println("任务结果:" result);
  14. }
  15. }

实现Callabe接口,可以获取线程执行的结果,FutureTask实际上实现了Runnable接口。

定时器(java.util.Timer)

  1. public class CreatingThread05 {
  2. public static void main(String[] args) {
  3. Timer timer = new Timer();
  4. // 每隔1秒执行一次
  5. timer.schedule(new TimerTask() {
  6. @Override
  7. public void run() {
  8. System.out.println(Thread.currentThread().getName() " is running");
  9. }
  10. }, 0 , 1000);
  11. }
  12. }

使用定时器java.util.Timer可以快速地实现定时任务,TimerTask实际上实现了Runnable接口。

线程池

  1. public class CreatingThread06 {
  2. public static void main(String[] args) {
  3. ExecutorService threadPool = Executors.newFixedThreadPool(5);
  4. for (int i = 0; i < 100; i ) {
  5. threadPool.execute(()-> System.out.println(Thread.currentThread().getName() " is running"));
  6. }
  7. }
  8. }

使用线程池的方式,可以复用线程,节约系统资源。

并行计算(Java8 )

  1. public class CreatingThread07 {
  2. public static void main(String[] args) {
  3. List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
  4. // 串行,打印结果为12345
  5. list.stream().forEach(System.out::print);
  6. System.out.println();
  7. // 并行,打印结果随机,比如35214
  8. list.parallelStream().forEach(System.out::print);
  9. }
  10. }

使用并行计算的方式,可以提高程序运行的效率,多线程并行执行。

Spring异步方法

首先,springboot启动类加上@EnableAsync注解(@EnableAsync是spring支持的,这里方便举例使用springboot)。

  1. @SpringBootApplication
  2. @EnableAsync
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

其次,方法加上@Async注解。

  1. @Service
  2. public class CreatingThread08Service {
  3. @Async
  4. public void call() {
  5. System.out.println(Thread.currentThread().getName() " is running");
  6. }
  7. }

然后,测试用例直接跟使用一般的Service方法一模一样。

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(classes = Application.class)
  3. public class CreatingThread08Test {
  4. @Autowired
  5. private CreatingThread08Service creatingThread08Service;
  6. @Test
  7. public void test() {
  8. creatingThread08Service.call();
  9. creatingThread08Service.call();
  10. creatingThread08Service.call();
  11. creatingThread08Service.call();
  12. }
  13. }

运行结果如下:

  1. task-3 is running
  2. task-2 is running
  3. task-1 is running
  4. task-4 is running

可以看到每次执行方法时使用的线程都不一样。

使用Spring异步方法的方式,可以说是相当地方便,适用于前后逻辑不相关联的适合用异步调用的一些方法,比如发送短信的功能。

总结

(1)继承Thread类并重写run()方法;

(2)实现Runnable接口;

(3)匿名内部类;

(4)实现Callabe接口;

(5)定时器(java.util.Timer);

(6)线程池;

(7)并行计算(Java8 );

(8)Spring异步方法;

彩蛋

上面介绍了那么多创建线程的方式,其实本质上就两种,一种是继承Thread类并重写其run()方法,一种是实现Runnable接口的run()方法,那么它们之间到底有什么联系呢?

请看下面的例子,同时继承Thread并实现Runnable接口,应该输出什么呢?

  1. public class CreatingThread09 {
  2. public static void main(String[] args) {
  3. new Thread(()-> {
  4. System.out.println("Runnable: " Thread.currentThread().getName());
  5. }) {
  6. @Override
  7. public void run() {
  8. System.out.println("Thread: " getName());
  9. }
  10. }.start();
  11. }
  12. }

说到这里,我们有必要看一下Thread类的源码:

  1. public class Thread implements Runnable {
  2. // Thread维护了一个Runnable的实例
  3. private Runnable target;
  4. public Thread() {
  5. init(null, null, "Thread-" nextThreadNum(), 0);
  6. }
  7. public Thread(Runnable target) {
  8. init(null, target, "Thread-" nextThreadNum(), 0);
  9. }
  10. private void init(ThreadGroup g, Runnable target, String name,
  11. long stackSize, AccessControlContext acc,
  12. boolean inheritThreadLocals) {
  13. // ...
  14. // 构造方法传进来的Runnable会赋值给target
  15. this.target = target;
  16. // ...
  17. }
  18. @Override
  19. public void run() {
  20. // Thread默认的run()方法,如果target不为空,会执行target的run()方法
  21. if (target != null) {
  22. target.run();
  23. }
  24. }
  25. }

看到这里是不是豁然开朗呢?既然上面的例子同时继承Thread并实现了Runnable接口,根据源码,实际上相当于重写了Thread的run()方法,在Thread的run()方法时实际上跟target都没有关系了。

所以,上面的例子输出结果为Thread: Thread-0,只输出重写Thread的run()方法中的内容。

#

欢迎关注我的公众号“彤哥读源码”,查看更多源码系列文章, 与彤哥一起畅游源码的海洋。

qrcode

发表评论

表情:
评论列表 (有 0 条评论,170人围观)

还没有评论,来说两句吧...

相关阅读