你是如何调用 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

}

  

posted on 2017-05-26 21:48  辰_雨_  阅读(240)  评论(0编辑  收藏  举报

导航