Object.wait
功能:挂起当前线程并释放锁
用途:用于多线程开发,主要用于条件队列编程。
用法:线程获取object锁后,调用object.wait,该线程被系统挂起,并释放锁。通过调用object.notify或者object.notifyAll可以唤醒该线程(调用notify或者notifyAll之前必须获得object锁)
唤醒:1,notify notifyAll
2,Thread.interrupt()
3,等待时间超时
一个线程也可以在不通知、不中断、不超时、不所谓的“虚假的唤醒”的情况下醒来。虽然在实践中很少出现这种情况,但应用程序必须通过测试导致线程被唤醒的条件,并在条件不满足的情况下继续等待。换句话说,等待应该总是出现在循环中,就像这样:
synchronized (obj) {
while (<condition does not hold>)
obj.wait(timeout);
... // Perform action appropriate to condition
}
wait(0)=wait() 线程被一直挂起,直到被唤醒
Thread.join()就是利用Object.wait实现的
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;
}
}
}
注意事项:
1,threadA调用threadB.join(),join中调用的wait不是让threadB挂起,而是让threadA挂起。
2,threadB结束的时候应该调用了notify或notifyAll(虽然Thread代码中没有找到),要不wait(0)会将threadA一直挂起。
JDK1.7 API文档的带参数的join方法的注释如下:
This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
这说明了thread结束的时候会调用notifyAll,虽然没有显式调用。
而且推荐不要在thread实例上调用wait,notify或者notifyAll