体现的就是保护性暂停模式。

join源码

  1. public final synchronized void join(long millis) throws InterruptedException {
  2. long base = System.currentTimeMillis();
  3. long now = 0;
  4. if (millis < 0) {
  5. throw new IllegalArgumentException("timeout value is negative");
  6. }
  7. if (millis == 0) {
  8. while (isAlive()) {
  9. wait(0);
  10. }
  11. } else {
  12. while (isAlive()) {
  13. long delay = millis - now;
  14. if (delay <= 0) {
  15. break;
  16. }
  17. wait(delay);
  18. now = System.currentTimeMillis() - base;
  19. }
  20. }
  21. }

t1.join() 等价于下面的代码

  1. synchronized (t1) {
  2. // 调用者线程进入 t1 的 waitSet 等待, 直到 t1 运行结束
  3. while (t1.isAlive()) {
  4. t1.wait(0);
  5. }
  6. }