1. 程式人生 > >LinkedBlockingQueue的put add跟offer的區別

LinkedBlockingQueue的put add跟offer的區別

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

                LinkedBlockingQueue的put,add和offer的區別 

      最近在學習<<Java併發程式設計實踐>>,有很多

java.util.concurrent包下的新類。LinkedBlockingQueue就是其中之一,顧名思義這是一個阻塞的執行緒安全的佇列,底層應該採用連結串列實現。

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

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

1.首先看一下add方法:

    Inserts 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方法在新增元素的時候,若超出了度列的長度會直接丟擲異常:

public 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方法:

    Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.

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

public 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方法:

     Inserts 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。

 

public 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]truetruefalse

 

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

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

 

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

remove:若佇列為空,丟擲NoSuchElementException異常。

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

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述