生活随笔
收集整理的這篇文章主要介紹了
阻塞队列BlockingQueue
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
BlockingQueue最終會有四種狀況,拋出異常、返回特殊值、阻塞、超時,下表總結了這些方法:
| ? | 拋出異常 | 特殊值 | 阻塞 | 超時 |
| 插入 | add(e) | offer(e) | put(e) | offer(e, time, unit) |
| 移除 | remove() | poll() | take() | poll(time, unit) |
| 檢查 | element() | peek() | 不可用 | 不可用 |
? ? ? ?
?
?
?
?
?
BlockingQueue是個接口,有如下實現類:
? ? ? ?1. ArrayBlockQueue:一個由數組支持的有界阻塞隊列。此隊列按 FIFO(先進先出)原則對元素進行排序。創建其對象必須明確大小,像數組一樣。
? ? ? ?2. LinkedBlockQueue:一個可改變大小的阻塞隊列。此隊列按 FIFO(先進先出)原則對元素進行排序。創建其對象如果沒有明確大小,默認值是Integer.MAX_VALUE。鏈接隊列的吞吐量通常要高于基于數組的隊列,但是在大多數并發應用程序中,其可預知的性能要低。?
? ? ? ?3.?PriorityBlockingQueue:類似于LinkedBlockingQueue,但其所含對象的排序不是FIFO,而是依據對象的自然排序順序或者是構造函數所帶的Comparator決定的順序。
? ? ? ?4.?SynchronousQueue:同步隊列。同步隊列沒有任何容量,每個插入必須等待另一個線程移除,反之亦然。
? ? ? ?下面使用ArrayBlockQueue來實現之前實現過的生產者消/費者模式,代碼如下:
/**?定義一個盤子類,可以放雞蛋和取雞蛋?*/??public?class?BigPlate?{?? /**?裝雞蛋的盤子,大小為5?*/??private?BlockingQueue<Object>?eggs?=?new?ArrayBlockingQueue<Object>(5);?? /**?放雞蛋?*/??public?void?putEgg(Object?egg)?{??try?{??eggs.put(egg);//?向盤子末尾放一個雞蛋,如果盤子滿了,當前線程阻塞??}?catch?(InterruptedException?e)?{??e.printStackTrace();??}?? //?下面輸出有時不準確,因為與put操作不是一個原子操作??System.out.println("放入雞蛋");??}?? /**?取雞蛋?*/??public?Object?getEgg()?{??Object?egg?=?null;??try?{??egg?=?eggs.take();//?從盤子開始取一個雞蛋,如果盤子空了,當前線程阻塞??}?catch?(InterruptedException?e)?{??e.printStackTrace();??}?? //?下面輸出有時不準確,因為與take操作不是一個原子操作??System.out.println("拿到雞蛋");??return?egg;??}?? /**?放雞蛋線程?*/??static?class?AddThread?extends?Thread?{??private?BigPlate?plate;??private?Object?egg?=?new?Object();?? public?AddThread(BigPlate?plate)?{??this.plate?=?plate;??}?? public?void?run()?{??plate.putEgg(egg);??}??}?? /**?取雞蛋線程?*/??static?class?GetThread?extends?Thread?{??private?BigPlate?plate;?? public?GetThread(BigPlate?plate)?{??this.plate?=?plate;??}?? public?void?run()?{??plate.getEgg();??}??}?? public?static?void?main(String[]?args)?{??BigPlate?plate?=?new?BigPlate();??//?先啟動10個放雞蛋線程??for(int?i?=?0;?i?<?10;?i++)?{??new?Thread(new?AddThread(plate)).start();??}??//?再啟動10個取雞蛋線程??for(int?i?=?0;?i?<?10;?i++)?{??new?Thread(new?GetThread(plate)).start();??}??}??} ?
轉載于:https://www.cnblogs.com/m2492565210/p/7468392.html
總結
以上是生活随笔為你收集整理的阻塞队列BlockingQueue的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。