上一篇學習了ExecutorService和其它相關類的源碼,本篇要學習的BlockingQueue中的源碼,as always,先上類圖java
其實繼承(實現)的層次比較簡單,咱們只要須要先學習一下BlockingQueue中的方法:學習
public interface BlockingQueue<E> extends Queue<E> { boolean add(E e);--往隊列中插入一個對象,隊列滿了會拋異常 boolean offer(E e);--同上,區別是隊列滿了會返回false void put(E e) throws InterruptedException;--插入一個元素,滿了就等待 boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException;--規定時間內插入元素 E take() throws InterruptedException;--彈出隊首 E poll(long timeout, TimeUnit unit) throws InterruptedException;--規定時間內彈出隊首 int remainingCapacity();--隊列剩餘大小 boolean remove(Object o);--刪除一個equals o的對象 public boolean contains(Object o);--是否包含o int drainTo(Collection<? super E> c);--把隊列遷移到另一個collection結構中 int drainTo(Collection<? super E> c, int maxElements);--遷移,有個最大遷移數量 }
其實除了poll和offer 其它方法通常咱們是用不到的,因此仍是很簡單的接口定義。下面去看一下具體的幾個實現類。對象
ArrayBlockingQueue--聲明時就肯定大小的隊列,fifo方式。(方法基本和接口一致,沒有特別要說明的內容)blog
LinkedBlockingQueue--鏈表實現的queue-remove效率會高一些繼承
PriorityBlockingQueue--優先級隊列接口
SynchronousQueue--阻塞隊列,必須拿走一個才能放進來一個,也就是最多隻有一個~隊列
DelayQuque--就是放進去的內容,延遲時間到了後才能夠得到ci
--rem
LinkedBlockDeque--雙端隊列 :offerFirst/offerLast,pollFirst/pollLast源碼
LinkedTransferQueue--相似LinkedUnBlockedQueue,其實就是transfer方法有人再等待隊列內容就直接給他這個元素,沒人在等就放在隊列裏面。也就是效率會更高。