示例代码:

运行结果:

1.作用
当前线程A执行了线程B的join方法之后,线程A会等待线程B结束后才会继续执行下面的代码;
2.源码解析


关键的是方法isAlive()和wait()方法
isAlive():当前线程已启动且没有死亡

wait():当前线程A释放线程B的锁,等待notify()、notifyAll()方法唤醒
但是这里并没有看到在哪里调用了线程B的notify()、notifyAll()方法,这个其实是在openJDK底层源码实现的,当一个线程run()之后,到最终结束运行时会调用该线程的notifyAll()方法,最终join()方法会返回,线程A继续执行下面的代码。
3.和CountDownLatch的区别
java - What is the difference between join and CountDownLatch? - Stack Overflow
4.参考资料
java - who and when notify the thread.wait() when thread.join() is called? - Stack Overflow
网友评论