join后语句先执行

join(long)与sleep(long)的区别

丶灬走出姿态 提交于 2019-12-06 10:43:57
1.join(long)方法的源码 首先看join()源码: public final void join() throws InterruptedException { join(0); } 从源码中可以看出,join()直接调用了join(long)方法,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; } } } 通过阅读源码可以看出,是通过不断轮询的方式去观察线程是否还是活动线程 join(0