简单示例
/**
* @author charles
* @date 2022/12/4 0:49
*/
public class ThreadJoinTest {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
// while (true){
System.out.println("t1 run");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// }
});
Thread t2 = new Thread(() -> {
System.out.println("t2 run");
});
Thread t3 = new Thread(() -> {
System.out.println("t3 run");
});
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
}
运行结果:
t1 run
t2 run
t3 run
原理分析
Thread#join(long)
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
主线程会持有当前线程对象的锁,然后调用wait方法去阻塞,而这个方法的调用者是在主线程中的。所以造成主线程阻塞。当线程执行结束,唤醒主线程。
应用场景
- 让多线程按照自己指定的顺序执行。
- 用join方法等待线程的执行结果,确保join方法的后续逻辑都在线程执行结束后处理。