麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

LinkedBlockingQueue的put,add跟offer的區(qū)別

2019-11-11 04:54:26
字體:
供稿:網(wǎng)友
LinkedBlockingQueue的put,add和offer的區(qū)別 

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

       看其API的時候發(fā)現(xiàn),添加元素的方法竟然有三個:add,put,offer。

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

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構(gòu)造的時候若沒有指定大小,則默認(rèn)大小為Integer.MAX_VALUE,當(dāng)然也可以在構(gòu)造函數(shù)的參數(shù)中指定大小。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();          }      }  //運行結(jié)果:  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方法,若向隊尾添加元素的時候發(fā)現(xiàn)隊列已經(jīng)滿了會發(fā)生阻塞一直等待空間,以加入元素。

[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();          }      }  //運行結(jié)果:  //在queue.put("yes")處發(fā)生阻塞  //下面的“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方法在添加元素時,如果發(fā)現(xiàn)隊列已滿無法添加的話,會直接返回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();          }      }  //運行結(jié)果:  [hello, world]  true  true  false  

 

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

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

 

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

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

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产一级免费电影 | 最新av免费网址 | 麻豆一区二区99久久久久 | 在线区 | 一级做受毛片免费大片 | 日韩精品免费看 | 国产精品久久久久久久久久10秀 | 91久久91久久精品免观看 | 黄污污网站 | 亚洲精品一区国产精品丝瓜 | 欧美一级电影网站 | 欧美成人免费电影 | 高清一区二区在线观看 | 国产精品久久久久av | 亚洲综合精品 | 亚洲自拍第二页 | 亚洲精久 | 乱淫67194 | 色婷婷av一区二区三区久久 | 久草手机视频在线观看 | 永久免费毛片 | 精品国产一区二区三区在线 | www.成人在线视频 | 一级免费在线视频 | 精品国产91久久久久 | 91精品国产综合久久男男 | 热久久91 | 精品一区二区久久久久久按摩 | 亚洲影院在线 | 国内自拍网址 | 精品久久999 | a级黄色片视频 | 久久综合给合久久狠狠狠97色69 | 成人羞羞在线观看网站 | av免费在线观看不卡 | 国产毛片aaa一区二区三区视频 | 欧美在线观看视频一区二区 | 成人一区三区 | 免费国产wwwwwww网站 | 久久久tv | 欧美大电影免费观看 |