http://www.javashuo.com/article/p-nidzvcyi-p.htmljava
//默認非公平阻塞隊列 ArrayBlockingQueue queue = new ArrayBlockingQueue(2); //公平阻塞隊列 ArrayBlockingQueue queue1 = new ArrayBlockingQueue(2,true);
public class ArrayBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, java.io.Serializable { /** 存儲數據的數組 */ final Object[] items; /**獲取數據的索引,主要用於take,poll,peek,remove方法 */ int takeIndex; /**添加數據的索引,主要用於 put, offer, or add 方法*/ int putIndex; /** 隊列元素的個數 */ int count; /** 控制並不是訪問的鎖 */ final ReentrantLock lock; /**notEmpty條件對象,用於通知take方法隊列已有元素,可執行獲取操做 */ private final Condition notEmpty; /**notFull條件對象,用於通知put方法隊列未滿,可執行添加操做 */ private final Condition notFull; /** 迭代器 */ transient Itrs itrs = null; }
LinkedBlockingQueuenode
public class LinkedBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, java.io.Serializable { /** * 節點類,用於存儲數據 */ static class Node<E> { E item; /** * One of: * - the real successor Node * - this Node, meaning the successor is head.next * - null, meaning there is no successor (this is the last node) */ Node<E> next; Node(E x) { item = x; } } /** 阻塞隊列的大小,默認爲Integer.MAX_VALUE */ private final int capacity; /** 當前阻塞隊列中的元素個數 */ private final AtomicInteger count = new AtomicInteger(); /** * 阻塞隊列的頭結點 */ transient Node<E> head; /** * 阻塞隊列的尾節點 */ private transient Node<E> last; /** 獲取並移除元素時使用的鎖,如take, poll, etc */ private final ReentrantLock takeLock = new ReentrantLock(); /** notEmpty條件對象,當隊列沒有數據時用於掛起執行刪除的線程 */ private final Condition notEmpty = takeLock.newCondition(); /** 添加元素時使用的鎖如 put, offer, etc */ private final ReentrantLock putLock = new ReentrantLock(); /** notFull條件對象,當隊列數據已滿時用於掛起執行添加的線程 */ private final Condition notFull = putLock.newCondition(); }