// wait thread
synchronized (syncObject) {
syncObject.wait(); // to proceed to next line,
// this thread must wait until notify is called
// and then take ownership over syncObject
// next line
}
...
// notify thread
synchronized (syncObject) {
syncObject.wait();
while (true) {}; // infinite loop, syncObject is never released,
// wait thread will never gain ownership over syncObject
// and will never wake up
}
# 1 楼答案
wait
(以及notify
)需要使用同步监视器。如果调用notify
的代码没有释放它,那么wait
将继续“等待”直到释放监视器