【发布时间】:2014-09-04 18:15:06
【问题描述】:
来自 jdk 1.7.0_45 Thread.join(long miilliseconds) 的实例方法通过使调用者线程在此线程的对象监视器上等待来工作。此外,javadoc 明确指出
As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
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;
}
wai t(delay);
now = System.currentTimeMillis() - base;
}
}
}
我没有看到 notifyAll() 被调用,因此调用 join() 的线程获取了该线程对象的监视器
如果我在线程 t 上调用 t.join(0),那么我没有在我的 run() 代码中实现 notifyAll()。那么调用者线程如何(调用 t.join() 的线程得到通知)
【问题讨论】:
标签: java multithreading