PriorityBlockingQueue是一個支持優先級的無界阻塞隊列,直到系統資源耗盡。默認情況下元素采用自然順序升序排列。也可以自定義類實現compareTo()方法來指定元素排序規則,或者初始化PriorityBlockingQueue時,指定構造參數Comparator來對元素進行排序。但需要注意的是不能保證同優先級元素的順序。PriorityBlockingQueue也是基於最小二叉堆實現,使用基於CAS實現的自旋鎖來控制隊列的動態擴容,保證了擴容操作不會阻塞take操作的執行。
PriorityBlockingQueue有四個構造方法:
// 默認的構造方法,該方法會調用this(DEFAULT_INITIAL_CAPACITY, null),即默認的容量是11
public PriorityBlockingQueue()
// 根據initialCapacity來設置隊列的初始容量
public PriorityBlockingQueue(int initialCapacity)
// 根據initialCapacity來設置隊列的初始容量,並根據comparator對象來對數據進行排序
public PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator)
// 根據集合來創建隊列
public PriorityBlockingQueue(Collection<? extends E> c)
public class PriorityBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, java.io.Serializable { private static final long serialVersionUID = 5595510919245408276L; private static final int DEFAULT_INITIAL_CAPACITY = 11; private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; private transient Object[] queue; private transient int size; private transient Comparator<? super E> comparator; private final ReentrantLock lock; private final Condition notEmpty; private transient volatile int allocationSpinLock;//擴容時候用到,自旋鎖 private PriorityQueue<E> q;//數組實現的最小堆,writeObject和readObject用到。 為了兼容之前的版本,只有在序列化和反序列化才非空 public PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator) { if (initialCapacity < 1) throw new IllegalArgumentException(); this.lock = new ReentrantLock(); this.notEmpty = lock.newCondition(); this.comparator = comparator; this.queue = new Object[initialCapacity]; //構造函數沒有初始化allocationSpinLock,q } public PriorityBlockingQueue(Collection<? extends E> c) { this.lock = new ReentrantLock(); this.notEmpty = lock.newCondition(); boolean heapify = true; // true if not known to be in heap order boolean screen = true; // true if must screen for nulls if (c instanceof SortedSet<?>) {// 如果傳入集合是有序集,則無須進行堆有序化 SortedSet<? extends E> ss = (SortedSet<? extends E>) c; this.comparator = (Comparator<? super E>) ss.comparator(); heapify = false;//不需要重建堆 }// 如果傳入集合是PriorityBlockingQueue類型,則不進行堆有序化 else if (c instanceof PriorityBlockingQueue<?>) { PriorityBlockingQueue<? extends E> pq = (PriorityBlockingQueue<? extends E>) c; this.comparator = (Comparator<? super E>) pq.comparator(); screen = false; if (pq.getClass() == PriorityBlockingQueue.class) // exact match heapify = false;//不需要重建堆 } Object[] a = c.toArray(); int n = a.length; // If c.toArray incorrectly doesn't return Object[], copy it. if (a.getClass() != Object[].class) a = Arrays.copyOf(a, n, Object[].class); if (screen && (n == 1 || this.comparator != null)) { for (int i = 0; i < n; ++i) if (a[i] == null) throw new NullPointerException(); } this.queue = a; this.size = n; if (heapify) heapify();//重建堆 } private void removeAt(int i) { Object[] array = queue; int n = size - 1; if (n == i) // removed last element array[i] = null; else { E moved = (E) array[n]; array[n] = null; Comparator<? super E> cmp = comparator; if (cmp == null) siftDownComparable(i, moved, array, n); else siftDownUsingComparator(i, moved, array, n, cmp); if (array[i] == moved) { if (cmp == null) siftUpComparable(i, moved, array); else siftUpUsingComparator(i, moved, array, cmp); } } size = n; } private static <T> void siftDownComparable(int k, T x, Object[] array, int n) {//元素x放到k的位置 if (n > 0) { Comparable<? super T> key = (Comparable<? super T>)x; int half = n >>> 1; // loop while a non-leaf while (k < half) { int child = (k << 1) + 1; // assume left child is least Object c = array[child]; int right = child + 1; if (right < n && ((Comparable<? super T>) c).compareTo((T) array[right]) > 0) c = array[child = right]; if (key.compareTo((T) c) <= 0)//比子節點小就不動,小堆 break; array[k] = c; k = child; } array[k] = key; } } private static <T> void siftUpComparable(int k, T x, Object[] array) {//元素x放到k的位置 Comparable<? super T> key = (Comparable<? super T>) x; while (k > 0) { int parent = (k - 1) >>> 1; Object e = array[parent]; if (key.compareTo((T) e) >= 0)//比父親大就不動,小堆 break; array[k] = e; k = parent; } array[k] = key; } public boolean offer(E e) { if (e == null)// 若插入的元素為null,則直接拋出NullPointerException異常 throw new NullPointerException(); final ReentrantLock lock = this.lock; lock.lock(); int n, cap; Object[] array; while ((n = size) >= (cap = (array = queue).length)) tryGrow(array, cap); try { Comparator<? super E> cmp = comparator; if (cmp == null) siftUpComparable(n, e, array);//准備放在最后size位置處 else siftUpUsingComparator(n, e, array, cmp); size = n + 1; notEmpty.signal();// 喚醒等待在空上的線程 } finally { lock.unlock(); } return true; } public E take() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); E result; try { while ( (result = dequeue()) == null) notEmpty.await(); } finally { lock.unlock(); } return result; } public E poll(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); E result; try { while ( (result = dequeue()) == null && nanos > 0) nanos = notEmpty.awaitNanos(nanos); } finally { lock.unlock(); } return result; } public E peek() { final ReentrantLock lock = this.lock; lock.lock(); try { return (size == 0) ? null : (E) queue[0]; } finally { lock.unlock(); } } public int size() { final ReentrantLock lock = this.lock; lock.lock(); try { return size; } finally { lock.unlock(); } } private int indexOf(Object o) { if (o != null) { Object[] array = queue; int n = size; for (int i = 0; i < n; i++) if (o.equals(array[i])) return i; } return -1; } public boolean remove(Object o) { final ReentrantLock lock = this.lock; lock.lock(); try { int i = indexOf(o); if (i == -1) return false; removeAt(i); return true; } finally { lock.unlock(); } } public boolean contains(Object o) { final ReentrantLock lock = this.lock; lock.lock(); try { return indexOf(o) != -1; } finally { lock.unlock(); } } private E dequeue() { int n = size - 1; if (n < 0) return null; else { Object[] array = queue; E result = (E) array[0]; E x = (E) array[n]; array[n] = null; Comparator<? super E> cmp = comparator; if (cmp == null) siftDownComparable(0, x, array, n); else siftDownUsingComparator(0, x, array, n, cmp); size = n; return result; } } private void heapify() { Object[] array = queue; int n = size; int half = (n >>> 1) - 1; Comparator<? super E> cmp = comparator; if (cmp == null) { for (int i = half; i >= 0; i--) siftDownComparable(i, (E) array[i], array, n);//數組重建為堆 } else { for (int i = half; i >= 0; i--) siftDownUsingComparator(i, (E) array[i], array, n, cmp); } } public void clear() { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] array = queue; int n = size; size = 0; for (int i = 0; i < n; i++) array[i] = null; } finally { lock.unlock(); } public int drainTo(Collection<? super E> c, int maxElements) {//批量獲取元素 if (c == null) throw new NullPointerException(); if (c == this) throw new IllegalArgumentException(); if (maxElements <= 0) return 0; final ReentrantLock lock = this.lock; lock.lock(); try { int n = Math.min(size, maxElements); for (int i = 0; i < n; i++) {// 循環遍歷,不斷彈出隊首元素; c.add((E) queue[0]); // In this order, in case add() throws. dequeue(); } return n; } finally { lock.unlock(); } } }
放,取,移除 的時候都加鎖,同時只能一個線程操作。
private PriorityQueue<E> q;//數組實現的最小堆,writeObject和readObject用到。
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { lock.lock(); try { // avoid zero capacity argument q = new PriorityQueue<E>(Math.max(size, 1), comparator); q.addAll(this); s.defaultWriteObject(); } finally { q = null; lock.unlock(); } } private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { try { s.defaultReadObject(); int sz = q.size(); SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, sz); this.queue = new Object[sz]; comparator = q.comparator(); addAll(q); } finally { q = null; } }
private transient volatile int allocationSpinLock;//擴容時候用到
不擴容就是正常的獲取鎖之后加入元素。
擴容時候釋放了鎖,如果取的線程獲取了鎖可以取,如果offer的線程獲取了鎖可以放(方法中釋放了鎖,別的線程就可以進去這個方法,也可以進去其他需要鎖的方法)
釋放了lock鎖加了一把allocationSpinLock 鎖(這個鎖:獲取到的走進去,沒有獲取到的跳過。)
private void tryGrow(Object[] array, int oldCap) {//舊數組和容量 lock.unlock(); // 釋放鎖,防止阻塞出隊操作 Object[] newArray = null; //釋放了鎖,多個線程可以進來這里,但是只有一個線程可以執行if里面的代碼,也就是只有一個線程可以擴容, if (allocationSpinLock == 0 && // 使用CAS操作來修改allocationSpinLock UNSAFE.compareAndSwapInt(this, allocationSpinLockOffset, 0, 1)) { try {// 容量越小增長得越快,若容量小於64,則新容量是oldCap * 2 + 2,否則是oldCap * 1.5 int newCap = oldCap + ((oldCap < 64) ? (oldCap + 2) : // grow faster if small (oldCap >> 1)); if (newCap - MAX_ARRAY_SIZE > 0) { // 擴容后超過最大容量處理 int minCap = oldCap + 1; if (minCap < 0 || minCap > MAX_ARRAY_SIZE)//整數溢出 throw new OutOfMemoryError(); newCap = MAX_ARRAY_SIZE; }//queue是公共變量, if (newCap > oldCap && queue == array) newArray = new Object[newCap]; } finally {// 解鎖,因為只有一個線程到此,因而不需要CAS操作; allocationSpinLock = 0; } }//失敗擴容的線程newArray == null,調用Thread.yield()讓出cpu, 讓擴容線程擴容后優先調用lock.lock重新獲取鎖, //但是這得不到一定的保證,有可能調用Thread.yield()的線程先獲取了鎖。 if (newArray == null) Thread.yield(); lock.lock();//有可能擴容的線程先走到這里,也有可能沒有擴容的線程先走到這里。 //准備賦值給共有變量queue,要加鎖, //擴容的線程newArray != null ,沒有擴容的線程newArray = null if (newArray != null && queue == array) {//再次進入while循環去擴容。 queue = newArray; System.arraycopy(array, 0, newArray, 0, oldCap); } } private static final sun.misc.Unsafe UNSAFE; private static final long allocationSpinLockOffset; static { try { UNSAFE = sun.misc.Unsafe.getUnsafe(); Class<?> k = PriorityBlockingQueue.class; allocationSpinLockOffset = UNSAFE.objectFieldOffset (k.getDeclaredField("allocationSpinLock")); //allocationSpinLock這個字段 } catch (Exception e) { throw new Error(e); } }
PriorityBlockingQueue擴容時,因為增加堆數組的長度並不影響隊列中元素的出隊操作,因而使用自旋CAS操作實現的鎖來控制擴容操作,僅在數組引用替換和拷貝元素時才加鎖,從而減少了擴容對出隊操作的影響。
數組變成Iterator取遍歷:
public Iterator<E> iterator() { return new Itr(toArray()); } public Object[] toArray() { final ReentrantLock lock = this.lock; lock.lock(); try { return Arrays.copyOf(queue, size); } finally { lock.unlock(); } } final class Itr implements Iterator<E> { final Object[] array; // Array of all elements int cursor; // index of next element to return int lastRet; // index of last element, or -1 if no such Itr(Object[] array) { lastRet = -1; this.array = array; } public boolean hasNext() { return cursor < array.length; } public E next() { if (cursor >= array.length) throw new NoSuchElementException(); lastRet = cursor; return (E)array[cursor++]; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); removeEQ(array[lastRet]); lastRet = -1; } }
PriorityBlockingQueue中查找元素的效率indexOf()是偏低的,由於二叉堆並沒有限制左右子節點的大小規則,因而需要變量整個數組進行查找,因而效率為O(n)。一些優先隊列的實現會對此進行優化,給每個元素添加一個索引字段用於標記元素在堆數組中的位置,比如:ScheduledThreadPoolExecutor.DelayedWorkQueue通過ScheduledFutureTask中的heapIndex來標記任務在堆數組中的位置。
PBQSpliterator沒看