你是如何调用 wait()方法的?使用 if 块还是循环?为什么
应该使在循环调用,当线程获取到CPU开始执行的时候,其他条件可能还没有满足,所以在处理前
循环检测条件是最好的满足条件会更好。如下代码:
// The standard idiom for using the wait method
synchronized (obj) {
while (condition does not hold)
obj.wait(); // (Releases lock, and reacquires on wakeup)
... // Perform action appropriate to condition
}
浙公网安备 33010602011771号