wait和notify函数的规范代码模板

// The standard idiom for calling the wait method in Java 
synchronized (sharedObject) { 
    while (condition) {
        sharedObject.wait(); 
        // (Releases lock, and reacquires on wakeup) 
    } 
    // do action based upon condition e.g. take or put into queue 
} 

⑤多线程中测试某个条件的变化用 if 还是用 while?

说是要把if换成while的理由,应该是wait线程被唤醒之后,会继续从wait那里往下面执行,如果是if的话,就会直接往下面执行,不会再判断if的状态了;但是如果是while的话,从wait那里往下面执行,然后还会判断while中的条件,如果==0的话,还是会继续阻塞,如果是>0,则会执行while语句块外面的remove吧?!其中关键就是被notify唤醒之后,是否会执行条件判断

 

http://www.cnblogs.com/hapjin/p/5492645.html

原文地址:https://www.cnblogs.com/H-BolinBlog/p/6911075.html