十六、线程强制执行

  • Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞
  • 可以了解成插队

 

public class ThreadJoin implements Runnable{

    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        for (int i = 1; i <= 10; i++) {
            System.out.println("插队线程"+i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadJoin t = new ThreadJoin();
        Thread thread = new Thread(t);
        thread.start();
        for (int i = 1; i <= 20; i++) {
            if (i==10){
                thread.join();
            }
            System.out.println("主线程"+i);
        }
    }
}

结果:

posted @ 2022-05-14 21:02  Epiphany8Z  阅读(22)  评论(0编辑  收藏  举报