J.U.C包含許多併發經常使用的容器類工具,好比ConcurrentHashMap、CopyOnWriteArrayList。由與隊列Queue在併發中很是重要,而且種類很是多,故本文將隊列單獨列出,不計算在容器類中。java
1,ConcurrentHashMap,併發Map。併發編程中最經常使用的併發容器。JDK1.8中的底層實現與以前版本不一樣。在API的使用上和java.lang.HashMap相同。須要注意的是,HashMap支持null做爲key,而ConcurrentHashMap不支持,會報空指針異常。ConcurrentHashMap在遍歷的時候沒有FailFast機制。編程
2,CopyOnWriteArrayList,支持併發讀寫的List。在API的使用上和java.lang.ArrayList相同。容許一個線程寫的同時多個線程進行讀操做。CopyOnWriteArrayList在遍歷的時候沒有FailFast機制。數組
隊列是併發中常常出現的結構。隊列包括阻塞隊列BlockingQueue、雙端阻塞隊列BlockingDequeue、併發隊列(非阻塞)ConcurrentLinkedQueue等。遵循FIFO先入先出的原則。安全
BlockingQueue接口的經常使用API以下:併發
boolean add(E e); // 增長元素。若是隊列已滿,拋IllegalStateException異常 void put(E e) throws InterruptedException; // 增長元素。若是隊列已滿,則阻塞等待。阻塞期間響應線程中斷異常。 boolean offer(E e); // 增長元素。若是隊列已滿,直接返回false。 boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException // 增長元素。若是隊列已滿,則阻塞等待一段時間。阻塞期間響應線程中斷異常。返回值同offer(E e) boolean remove(Object o); // 刪除指定元素。若是隊列發生了改變,則返回true。 E take() throws InterruptedException; // 獲取元素。若是隊列爲空則阻塞等待。阻塞期間響應線程中斷異常。 E poll(long timeout, TimeUnit unit) throws InterruptedException; // 指定時間內阻塞等待獲取元素。若是隊列爲空則阻塞等待。阻塞期間響應線程中斷異常。 boolean contains(Object o); // 隊列中是否包含指定元素 int remainingCapacity(); // 隊列還能存放多少個元素,返回值 = 初始化總數 - 以存放個數
BlockingQueue接口的實現類有:ArrayBlockingQueue、DelayQueue、LinkedBlockingQueue、PriorityBlockingQueue、SynchronousQueue。工具
1,ArrayBlockingQueue,數組阻塞隊列。ArrayBlockingQueue 是一個有界的阻塞隊列,構造方法能夠指定是不是公平性。其內部實現是將對象放到一個數組裏。有界也就意味着,它不可以存儲無限多數量的元素。它有一個同一時間可以存儲元素數量的上限。你能夠在對其初始化的時候設定這個上限,但以後就沒法對這個上限進行修改了 (譯者注:由於它是基於數組實現的,也就具備數組的特性:一旦初始化,大小就沒法修改)。ArrayBlockingQueue 內部以 FIFO(先進先出)的順序對元素進行存儲。隊列中的頭元素在全部元素之中是放入時間最久的那個,而尾元素則是最短的那個。線程
2,DelayQueue,延遲隊列。DelayQueue 對元素進行持有直到一個特定的延遲到期。注入其中的元素必須實現java.util.concurrent.Delayed接口。給接口繼承了java.lang.Comarable比較器接口,具體以下:指針
public interface Delayed extends Comparable<Delayed> { long getDelay(TimeUnit unit); }
DelayQueue 將會在每一個元素的getDelay()方法返回的值的時間段以後才加入到隊列中。若是返回的是 0 或者負值,則直接加入到隊列中。code
3,LinkedBlockingQueue,鏈表阻塞隊列。LinkedBlockingQueue是效率最高的、出鏡率最高的阻塞隊列,內部以一個鏈式結構(連接節點)對其元素進行存儲。若是須要的話,這一鏈式結構能夠選擇一個上限。若是沒有定義上限,將使用 Integer.MAX_VALUE 做爲上限。對象
4,PriorityBlockingQueue,優先級隊列。PriorityBlockingQueue是一個無界的併發隊列。它使用了和ava.util.PriorityQueue 同樣的排序規則。全部插入到 PriorityBlockingQueue 的元素必須實現java.lang.Comparable接口,由於元素的優先級比較是經過Comparable接口來完成的。列中元素的排序就取決於你本身的 Comparable 實現。tips:從PriorityBlockingQueue隊列返回的Iterator並不能保證元素的遍歷是元素的優先級順序的。
5,SynchronousQueue,同步隊列。SynchronousQueue是一個特殊的隊列,它的內部同時只可以容納1個元素。若是該隊列已 有一元素的話,試圖向隊列中插入一個新元素的線程將會阻塞,直到另外一個線程將該元素從隊列中抽走。一樣,若是該隊列爲空,試圖向隊列中抽取一個元素的線程將會阻塞,直到另外一個線程向隊列中插入了一條新的元素。
6,LinkedTransferQueue,無界鏈表隊列。相比於LinkedBlockingQueue,多了tryTransfer和transfer方法。
void transfer(E e) throws InterruptedException // 若是有線程在阻塞等待回去元素,則直接將e傳遞給等待的線程。若是沒有則阻塞等待線程來獲取元素。阻塞期間響應線程中斷異常。(違反了FIFO原則) boolean tryTransfer(E e, long timeout, TimeUnit unit)throws InterruptedException // 一段時間內阻塞等待線程獲取元素,做用同transfer(E e)。若是超時則返回false。
BlockingDeque是雙端阻塞隊列接口,容許在隊列的兩端進元素進行添加和獲取。繼承類BlockingQueue接口,可是不推薦使用BlockingQueue相關的API。BlockingDeque的經常使用API以下:
//在頭部操做 boolean addFirst(E e); // 同BlockingQueue的add(E e) void putFirst(E e) throws InterruptedException; // 同BlockingQueue的put(E e) boolean offerFirst(E e); // 同BlockingQueue的offer(E e) boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException // 同BlockingQueue的offer(E e, long timeout, TimeUnit unit) boolean removeFirstOccurrence(Object o); // 從頭部開始刪除第一個相同的元素。同BlockingQueue的remove(E e) E takeFirst() throws InterruptedException; // 同BlockingQueue的take() E pollFirst(long timeout, TimeUnit unit) throws InterruptedException; // 同BlockingQueue的poll() // 在尾部操做 boolean addLast(E e); // 同BlockingQueue的add(E e) void putLast(E e) throws InterruptedException; // 同BlockingQueue的put(E e) boolean offerLast(E e); // 同BlockingQueue的offer(E e) boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException // 同BlockingQueue的offer(E e, long timeout, TimeUnit unit) boolean removeLastOccurrence(Object o); // 從尾部開始刪除第一個相同的元素。同BlockingQueue的remove(E e) E takeLast() throws InterruptedException; // 同BlockingQueue的take() E pollLast(long timeout, TimeUnit unit) throws InterruptedException; // 同BlockingQueue的poll()
7,LinkedBlockingDeque,鏈表雙端阻塞隊列。J.U.C包中BlockingDeque接口的惟一實現類。其實現與LinkedBlockingQueue相同。當鏈表爲空的時候,一個試圖從中抽取數據的線程將會阻塞,不管該線程是試圖從哪一端抽取數據。建議使用Deque進行多態形式的API操做。
8,ConcurrentLinkedQueue,併發非阻塞安全隊列。ConcurrentLinkedQueue實現了Queue接口,是一個基於鏈表的無界隊列,遵循FIFO先進先出原則,使用循環CAS的方式來實現非阻塞併發安全。建議使用Queue進行多態形式的API操做。