publicArrayBlockingQueue(int capacity){this(capacity,false);}publicArrayBlockingQueue(int capacity,boolean fair){if(capacity <&#61;0)thrownewIllegalArgumentException();this.items &#61;newObject[capacity];lock &#61;newReentrantLock(fair);notEmpty &#61; lock.newCondition();notFull &#61; lock.newCondition();}publicArrayBlockingQueue(int capacity,boolean fair,Collection<?extendsE> c){this(capacity, fair);finalReentrantLock lock &#61;this.lock;lock.lock();// Lock only for visibility, not mutual exclusiontry{int i &#61;0;try{for(E e : c){checkNotNull(e);items[i&#43;&#43;]&#61; e;}}catch(ArrayIndexOutOfBoundsException ex){thrownewIllegalArgumentException();}count &#61; i;putIndex &#61;(i &#61;&#61; capacity)?0: i;}finally{lock.unlock();}}
LinkedBlockingQueue
构造函数
publicLinkedBlockingQueue(){this(Integer.MAX_VALUE);}publicLinkedBlockingQueue(int capacity){if(capacity <&#61;0)thrownewIllegalArgumentException();this.capacity &#61; capacity;last &#61; head &#61;newNode<E>(null);}publicLinkedBlockingQueue(Collection<?extendsE> c){this(Integer.MAX_VALUE);finalReentrantLock putLock &#61;this.putLock;putLock.lock();// Never contended, but necessary for visibilitytry{int n &#61;0;for(E e : c){if(e &#61;&#61;null)thrownewNullPointerException();if(n &#61;&#61; capacity)thrownewIllegalStateException("Queue full");enqueue(newNode<E>(e));&#43;&#43;n;}count.set(n);}finally{putLock.unlock();}}
PriorityBlockingQueue
构造函数
publicPriorityBlockingQueue(){this(DEFAULT_INITIAL_CAPACITY,null);}publicPriorityBlockingQueue(Collection<?extendsE> c){this.lock &#61;newReentrantLock();this.notEmpty &#61; lock.newCondition();boolean heapify &#61;true;// true if not known to be in heap orderboolean screen &#61;true;// true if must screen for nullsif(c instanceofSortedSet<?>){SortedSet<?extendsE> ss &#61;(SortedSet<?extendsE>) c;this.comparator &#61;(Comparator<?superE>) ss.comparator();heapify &#61;false;}elseif(c instanceofPriorityBlockingQueue<?>){PriorityBlockingQueue<?extendsE> pq &#61;(PriorityBlockingQueue<?extendsE>) c;this.comparator &#61;(Comparator<?superE>) pq.comparator();screen &#61;false;if(pq.getClass()&#61;&#61;PriorityBlockingQueue.class)// exact matchheapify &#61;false;}Object[] a &#61; c.toArray();int n &#61; a.length;// If c.toArray incorrectly doesn&#39;t return Object[], copy it.if(a.getClass()!&#61;Object[].class)a &#61;Arrays.copyOf(a, n,Object[].class);if(screen &&(n &#61;&#61;1||this.comparator !&#61;null)){for(int i &#61;0; i < n;&#43;&#43;i)if(a[i]&#61;&#61;null)thrownewNullPointerException();}this.queue &#61; a;this.size &#61; n;if(heapify)heapify();}publicPriorityBlockingQueue(int initialCapacity){this(initialCapacity,null);}publicPriorityBlockingQueue(int initialCapacity,Comparator<?superE> comparator){if(initialCapacity <1)thrownewIllegalArgumentException();this.lock &#61;newReentrantLock();this.notEmpty &#61; lock.newCondition();this.comparator &#61; comparator;this.queue &#61;newObject[initialCapacity];}