java中关于join方法的源码是这样的:
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;
}
}
}
我不是太能理解这里isAlive和wait方法的使用。现在A中调用B.join,那么应该是判断B.siAlive然后A.wait()。但上面的代码似乎是B.isAlive()然后B.wait(),因为是在A中调用的B.join,那么上面代码的this应该是B啊。这是我理解有误吗?
还有另外一个问题,因为在一个线程结束后会调用notifyAll(),于是我有了以下代码:
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(ThreadJoinQuestion::action, "hehe");
t1.start();
t1.join();
}
private static void action(){
try {
for (int i = 0; i < 10; i++) {
Thread t2 = new Thread(ThreadJoinQuestion::action, "hehe");
t2.start();
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程正在执行");
}
private static void action2() {
System.out.println("线程2正在执行");
}
也就是在B线程中新建C线程,那么C线程结束后会调用notifyAll(),A线程不会被唤醒吗?因为在断点时join方法的while (isAlive())
只执行了两次:一次是在进入join()时,一次是在B线程结束时。说明C线程的结束并没有唤醒A线程,这是为什么?