java - 對(duì)于notify()/wait()的一點(diǎn)疑惑
問題描述
class MyObject{ private Queue<String> queue = new ConcurrentLinkedQueue<String>(); public synchronized void set(String s){ while(queue.size() >= 10){try { wait();} catch (InterruptedException e) { e.printStackTrace();} } queue.add(s); notify(); }}class Producer implements Runnable{ private MyObject myObj;public Producer(MyObject myObj) {this.myObj= myObj; } @Override public void run() {// 每條線程執(zhí)行30次setfor (int i = 0; i < 30; i++) { this.myObj.set('obj:' + i);} }}public static void main(String[] args){ Producer producer = new Producer(new MyObject()); // 生成30條線程 for (int i = 0; i < 10; i++) {Thread thread = new Thread(producer);thread.start(); } // 運(yùn)行結(jié)果是只set了30次}
我的疑惑是notify()發(fā)布通知,為什么不會(huì)讓其他線程的wait()方法繼續(xù)執(zhí)行下去呢?
問題解答
回答1:當(dāng)你隊(duì)列的數(shù)量大于10的時(shí)候, 你每個(gè)線程都是先wait()住了, 不會(huì)走到notify()的啊. 你需要一個(gè)單獨(dú)的線程去監(jiān)控隊(duì)列的大小, 大于10的時(shí)候notify(), 比如可以把你的稍微改一下
class MyObject { private Queue<String> queue = new ConcurrentLinkedQueue<String>(); private volatile int limit = 10; public synchronized void set(String s) { if (queue.size() >= limit) {try { wait();} catch (InterruptedException e) { e.printStackTrace();} } queue.add(s); } public synchronized void delta() { if (queue.size() >= limit) {limit += 10;notify(); } }}
然后有個(gè)監(jiān)控線程
class Monitor implements Runnable { private MyObject myObj; public Monitor(MyObject myObj) { this.myObj = myObj; } @Override public void run() { while (true) {myObj.delta(); } }}
相關(guān)文章:
1. javascript - webpack 報(bào)錯(cuò) 新人 求解2. nginx 80端口反向代理多個(gè)域名,怎樣隱藏端口的?3. angular.js - 關(guān)于ng-model和ng-bind的疑問4. node.js - 跑antd的的模板例子!想修改端口,怎么修改呢!!(里面好像用了什么dora插件!!!)5. windows-7 - Wamp集成環(huán)境Apache無法啟動(dòng)6. android - NavigationView 的側(cè)滑菜單中如何保存新增項(xiàng)(通過程序添加)7. angular.js - angular做點(diǎn)擊購(gòu)買時(shí)的遮罩層8. 關(guān)于thinkphp 5.1中,ajax提交數(shù)據(jù)url的格式寫法,加花括號(hào)就出錯(cuò),請(qǐng)老師指點(diǎn)9. 有大佬知道這種接口文件怎么使用嗎?10. tp5 不同控制器中的變量調(diào)用問題
