首页 > 其他 > 详细

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

时间:2017-05-27 09:33:06      阅读:332      评论:0      收藏:0      [点我收藏+]
// 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

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

原文:http://www.cnblogs.com/H-BolinBlog/p/6911075.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!