国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LinkedBlockingQueue的put,add跟offer的區別

2019-11-11 06:26:24
字體:
來源:轉載
供稿:網友
LinkedBlockingQueue的put,add和offer的區別 

      最近在學習<<java并發編程實踐>>,有很多java.util.concurrent包下的新類。LinkedBlockingQueue就是其中之一,顧名思義這是一個阻塞的線程安全的隊列,底層應該采用鏈表實現。

       看其API的時候發現,添加元素的方法竟然有三個:add,put,offer。

且這三個元素都是向隊列尾部添加元素的意思。于是我產生了興趣,要仔細探究一下他們之間的差別。

1.首先看一下add方法:

[java] view plain copyInserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.     This implementation returns true if offer succeeds, else throws an IllegalStateException.  

        LinkedBlockingQueue構造的時候若沒有指定大小,則默認大小為Integer.MAX_VALUE,當然也可以在構造函數的參數中指定大小。LinkedBlockingQueue不接受null。

       add方法在添加元素的時候,若超出了度列的長度會直接拋出異常:

[java] view plain copypublic static void main(String args[]){          try {              LinkedBlockingQueue<String> queue=new LinkedBlockingQueue(2);                            queue.add("hello");              queue.add("world");              queue.add("yes");          } catch (Exception e) {              // TODO: handle exception              e.PRintStackTrace();          }      }  //運行結果:  java.lang.IllegalStateException: Queue full      at java.util.AbstractQueue.add(Unknown Source)      at com.wjy.test.GrandPather.main(GrandPather.java:12)  

 

2.再來看一下put方法:

[java] view plain copyInserts the specified element at the tail of this queue, waiting if necessary for space to become available.  

      對于put方法,若向隊尾添加元素的時候發現隊列已經滿了會發生阻塞一直等待空間,以加入元素。

[java] view plain copypublic static void main(String args[]){          try {              LinkedBlockingQueue<String> queue=new LinkedBlockingQueue(2);                            queue.put("hello");              queue.put("world");              queue.put("yes");                            System.out.println("yes");          } catch (Exception e) {              // TODO: handle exception              e.printStackTrace();          }      }  //運行結果:  //在queue.put("yes")處發生阻塞  //下面的“yes”無法輸出  

 

3.最后看一下offer方法:

[java] view plain copyInserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue's capacity, returning true upon success and false if this queue is full. When using a capacity-restricted queue, this method is generally preferable to method add, which can fail to insert an element only by throwing an exception.  

   

    offer方法在添加元素時,如果發現隊列已滿無法添加的話,會直接返回false。

 

[java] view plain copypublic static void main(String args[]){          try {              LinkedBlockingQueue<String> queue=new LinkedBlockingQueue(2);                            boolean bol1=queue.offer("hello");              boolean bol2=queue.offer("world");              boolean bol3=queue.offer("yes");                            System.out.println(queue.toString());              System.out.println(bol1);              System.out.println(bol2);              System.out.println(bol3);          } catch (Exception e) {              // TODO: handle exception              e.printStackTrace();          }      }  //運行結果:  [hello, world]  true  true  false  

 

    好了,竟然說了這么多了,就把從隊列中取元素的方法也順便一說。

從隊列中取出并移除頭元素的方法有:poll,remove,take。

 

poll: 若隊列為空,返回null。

remove:若隊列為空,拋出NoSuchElementException異常。

take:若隊列為空,發生阻塞,等待有元素。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连山| 全州县| 临夏市| 武功县| 吴忠市| 庆阳市| 瓦房店市| 黄平县| 介休市| 增城市| 揭阳市| 江西省| 个旧市| 广昌县| 晋江市| 宝山区| 阜城县| 博罗县| 青岛市| 庐江县| 富宁县| 合肥市| 乳源| 沾益县| 临清市| 巍山| 开原市| 资兴市| 柳州市| 泌阳县| 石门县| 新丰县| 仙游县| 太湖县| 武清区| 沙湾县| 洪泽县| 宁陕县| 三原县| 堆龙德庆县| 张家川|