LinkedBlockingQueue繼承AbstractQueue,實現了BlockingQueue,Serializable接口。內部使用單向鏈表存儲數據。可定義容量,默認初始化容量是Integer最大值(可能出現內存溢出)。插入和取出使用不同的鎖,putLock插入鎖,takeLock取出鎖,添加和刪除數據的時候可以並行。多CPU情況下可以同一時刻既消費又生產。LinkedBlockingQueue在大多數併發的場景下吞吐量比ArrayBlockingQueue,但是性能不穩定。
private final ReentrantLock takeLock = new ReentrantLock();
/** Wait queue for waiting takes */
private final Condition notEmpty = takeLock.newCondition();
/** Lock held by put, offer, etc */
private final ReentrantLock putLock = new ReentrantLock();
/** Wait queue for waiting puts */
private final Condition notFull = putLock.newCondition();
構造函數
public LinkedBlockingQueue() {
this(Integer.MAX_VALUE);
}
public LinkedBlockingQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
this.capacity = capacity;
last = head = new Node<E>(null);
}
public LinkedBlockingQueue(Collection<? extends E> c) {
this(Integer.MAX_VALUE);
final ReentrantLock putLock = this.putLock;
putLock.lock(); // Never contended, but necessary for visibility
try {
int n = 0;
for (E e : c) {
if (e == null)
throw new NullPointerException();
if (n == capacity)
throw new IllegalStateException("Queue full");
enqueue(new Node<E>(e));
++n;
}
count.set(n);
} finally {
putLock.unlock();
}
}
常用方法
常用方法與ArrayBlockingQueue類似,方法具體實現可以參考源碼。ArrayBlockingQueue