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(); }