<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java線程(篇外篇):阻塞隊列BlockingQueue 好久沒有寫文章了,這段時間事情比較雜,工作也比較雜亂,上周日剛搬完家,從自建房搬到了樓房,提升了一層生活品質,哈哈!不過昨天晚上在公交車上錢包被偷了,前段時間還丟個自行車,不得不感嘆,京城扒手真多,還無人處理。言歸正傳,這一段時間我的工作主要是改進公司的調度器,調度器調度線程池執行任務,生產者生產任務,消費者消費任務,那么這時就需要一個任務隊列,生產者向隊列里插入任務,消費者從隊列里提取任務執行,調度器里是通過BlockingQueue實現的隊列,隨后小查一下,下面看看BlockingQueue的原理及其方法。 BlockingQueue最終會有四種狀況,拋出異常、返回特殊值、阻塞、超時,下表總結了這些方法: > | --? | *拋出異常* | *特殊值* | *阻塞* | *超時* | |---|---|---| > | **插入** | [`add(e)`](http://blog.csdn.net/ghsau/article/details/8108292) | [`offer(e)`](http://blog.csdn.net/ghsau/article/details/8108292) | [`put(e)`](http://blog.csdn.net/ghsau/article/details/8108292) | [`offer(e, time, unit)`](http://blog.csdn.net/ghsau/article/details/8108292) | > | **移除** | [`remove()`](http://blog.csdn.net/ghsau/article/details/8108292) | [`poll()`](http://blog.csdn.net/ghsau/article/details/8108292) | [`take()`](http://blog.csdn.net/ghsau/article/details/8108292) | [`poll(time, unit)`](http://blog.csdn.net/ghsau/article/details/8108292) | > | **檢查** | [`element()`](http://blog.csdn.net/ghsau/article/details/8108292) | [`peek()`](http://blog.csdn.net/ghsau/article/details/8108292) | *不可用* | *不可用* | BlockingQueue是個接口,有如下實現類: 1\. ArrayBlockQueue:一個由數組支持的有界阻塞隊列。此隊列按 FIFO(先進先出)原則對元素進行排序。創建其對象必須明確大小,像數組一樣。 2\. LinkedBlockQueue:一個可改變大小的阻塞隊列。此隊列按 FIFO(先進先出)原則對元素進行排序。創建其對象如果沒有明確大小,默認值是Integer.MAX_VALUE。鏈接隊列的吞吐量通常要高于基于數組的隊列,但是在大多數并發應用程序中,其可預知的性能要低。? 3.?PriorityBlockingQueue:類似于LinkedBlockingQueue,但其所含對象的排序不是FIFO,而是依據對象的自然排序順序或者是構造函數所帶的Comparator決定的順序。 4.?SynchronousQueue:同步隊列。同步隊列沒有任何容量,每個插入必須等待另一個線程移除,反之亦然。 下面使用ArrayBlockQueue來實現之前實現過的[生產者消/費者模式](http://blog.csdn.net/ghsau/article/details/7433673),代碼如下: ~~~ /**?定義一個盤子類,可以放雞蛋和取雞蛋?*/?? public?class?BigPlate?{?? ?? ????/**?裝雞蛋的盤子,大小為5?*/?? ????private?BlockingQueue?eggs?=?new?ArrayBlockingQueue(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();?? ????????}?? ????}?? }?? ~~~ 執行結果: ~~~ 放入雞蛋?? 放入雞蛋?? 放入雞蛋?? 放入雞蛋?? 放入雞蛋?? 拿到雞蛋?? 放入雞蛋?? 拿到雞蛋?? 拿到雞蛋?? 拿到雞蛋?? 放入雞蛋?? 放入雞蛋?? 放入雞蛋?? 拿到雞蛋?? 放入雞蛋?? 拿到雞蛋?? 拿到雞蛋?? 拿到雞蛋?? 拿到雞蛋?? 拿到雞蛋?? ~~~ 從結果看,啟動10個放雞蛋線程和10個取雞蛋線程,前5個放入雞蛋的線程成功執行,到第6個,發現盤子滿了,阻塞住,這時切換到取雞蛋線程執行,成功實現了生產者/消費者模式。java.util.concurrent包是個強大的包!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看