hooyantsing's Blog

14_线程强制执行_join

字数统计: 149阅读时长: 1 min
2020/08/21

14 线程强制执行_join

狂神说Java 多线程详解

阻塞当前线程,等待 join 方法的线程执行完后继续执行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package demo2;

public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("VIP " + i);
}
}

public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new TestJoin());
for (int i = 0; i < 100; i++) {
if(i == 50){
thread.start();
thread.join();
}
System.out.println("A" + i);
}
}
}

运行结果:

main线程执行到其他线程的join方法,main线程阻塞等待,等其他线程执行完毕,main现在0a9736c1ede6b709f7d71acb3ac4c571.png

CATALOG
  1. 1. 14 线程强制执行_join