java - 對(duì)于notify()/wait()的一點(diǎn)疑惑
問(wè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í)行下去呢?
問(wèn)題解答
回答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. python - 字符串中反斜杠的替換2. html - 求解關(guān)于偽類和visibility的問(wèn)題3. javascript - 在點(diǎn)擊nav后,用JS加上顏色,怎么在頁(yè)面跳轉(zhuǎn)后仍能保持改變后的顏色?4. 這段代碼是獲取百度收錄量的!需要怎么設(shè)置才能獲取百度快照旁邊的網(wǎng)址呢?5. vuejs組件內(nèi)的props的屬性賦值問(wèn)題?6. java - 關(guān)于表的主鍵問(wèn)題7. 誰(shuí)有mysql5.7安裝的詳細(xì)教程8. html5 - 如何讓H5頁(yè)面在手機(jī)瀏覽器里和微信全屏顯示9. java中使用log4j如何不用每次調(diào)用都聲明一下?10. Android的webView如何實(shí)現(xiàn)網(wǎng)頁(yè) 錄音功能?
