首页 > 编程语言 > 详细

7.join让3个线程顺序执行

时间:2021-07-17 18:18:42      阅读:19      评论:0      收藏:0      [点我收藏+]
package com.kpmg.thread;

public class TestJoin {

  public static void main(String[] args) throws InterruptedException {
    Thread t1 = new Thread(()->{
      for (int i = 0; i < 100; i++) {
        System.out.println("t1 start");
      }
    });
    Thread t2 = new Thread(()->{
      try {
        t1.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      for (int i = 0; i < 100; i++) {
        System.out.println("t2 start");
      }
    });
    Thread t3 = new Thread(()->{
      try {
        t2.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      for (int i = 0; i < 100; i++) {
        System.out.println("t3 start");
      }
    });
    t1.start();
    t2.start();
    t3.start();

    t3.join();
    System.out.println("主线程运行结束");
  }
}

 

7.join让3个线程顺序执行

原文:https://www.cnblogs.com/johnzhao/p/15024166.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!