put
public void put(E e) throws InterruptedException {
if (e == null) throw new NullPointerException();
// Note: convention in all put/take/etc is to preset local var
// holding count negative to indicate failure unless set.
int c = -1;
Node<E> node = new Node<E>(e);
final ReentrantLock putLock = this.putLock;
final AtomicInteger count = this.count;
// 獲取putLock等缀,可中斷
putLock.lockInterruptibly();
try {
/*
* Note that count is used in wait guard even though it is
* not protected by lock. This works because count can
* only decrease at this point (all other puts are shut
* out by lock), and we (or some other waiting put) are
* signalled if it ever changes from capacity. Similarly
* for all other uses of count in other wait guards.
*/
// notFull代表鬓长,鏈表未滿
// 這里如果鏈表已滿荸频,當然該條件不滿足就要等待
while (count.get() == capacity) {
notFull.await();
}
// 到這里我抠,說明鏈表還有空位铲觉,加到尾部
enqueue(node);
c = count.getAndIncrement();
// 如果當前鏈表還沒滿森缠,那么條件滿足祝闻,所以要喚醒游昼,無需再等待甘苍。
// 為什么是signal,而不是signalall呢酱床,本來就是一個一個添加的羊赵,全部喚醒只是浪費CPU而已。
if (c + 1 < capacity)
notFull.signal();
} finally {
// 釋放putLock
putLock.unlock();
}
// notEmpty條件代表扇谣,鏈表非空
// 如果c==0昧捷,說明鏈表中至少有一個node,那么條件滿足罐寨,所以要喚醒靡挥,無需繼續(xù)等待
if (c == 0)
signalNotEmpty();
}
offer
// 跟put類似,唯一不同是這里如果鏈表已滿鸯绿,那么會立即返回false
// 不會一直等到鏈表不滿為止
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
final AtomicInteger count = this.count;
// 當鏈表已滿跋破,那么直接返回false
if (count.get() == capacity)
return false;
int c = -1;
Node<E> node = new Node<E>(e);
final ReentrantLock putLock = this.putLock;
putLock.lock();
try {
// 當鏈表未滿簸淀,那么繼續(xù)執(zhí)行入列的操作
if (count.get() < capacity) {
enqueue(node);
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
}
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
return c >= 0;
}
poll
public E poll() {
final AtomicInteger count = this.count;
// 如果鏈表為空,直接返回null
if (count.get() == 0)
return null;
E x = null;
int c = -1;
final ReentrantLock takeLock = this.takeLock;
// 獲取takelock
takeLock.lock();
try {
// 如果鏈表不為空毒返,那么繼續(xù)
if (count.get() > 0) {
// head出棧
x = dequeue();
c = count.getAndDecrement();
// 如果當前鏈表不為空租幕,那么notEmpty條件滿足,喚醒
if (c > 1)
notEmpty.signal();
}
} finally {
takeLock.unlock();
}
// 如果當前鏈表移除一個的情況下拧簸,至少有一個空位劲绪,notfull條件滿足,有必要喚醒
if (c == capacity)
signalNotFull();
return x;
}
take
public E take() throws InterruptedException {
E x;
int c = -1;
final AtomicInteger count = this.count;
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
// 跟poll不同的是盆赤,這里如果鏈表為空的話贾富,會一直等待
while (count.get() == 0) {
notEmpty.await();
}
x = dequeue();
c = count.getAndDecrement();
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
if (c == capacity)
signalNotFull();
return x;
}
peek
public E peek() {
if (count.get() == 0)
return null;
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
// 返回頭節(jié)點,但并不移除它
Node<E> first = head.next;
if (first == null)
return null;
else
return first.item;
} finally {
takeLock.unlock();
}
}
remove
public boolean remove(Object o) {
if (o == null) return false;
// 拿鎖牺六,putlock+takelock
fullyLock();
try {
// 從head往后遍歷
for (Node<E> trail = head, p = trail.next;
p != null;
trail = p, p = p.next) {
// 如果能找到該對象颤枪,移除該節(jié)點
if (o.equals(p.item)) {
unlink(p, trail);
return true;
}
}
return false;
} finally {
// 解鎖淑际,takelock+putlock
fullyUnlock();
}
}
void fullyLock() {
putLock.lock();
takeLock.lock();
}
void fullyUnlock() {
takeLock.unlock();
putLock.unlock();
}
void unlink(Node<E> p, Node<E> trail) {
// assert isFullyLocked();
// p.next is not changed, to allow iterators that are
// traversing p to maintain their weak-consistency guarantee.
p.item = null;
// 跳過也就是孤立p節(jié)點
trail.next = p.next;
// 如果p是尾節(jié)點
if (last == p)
// 那么設置trail為尾節(jié)點,也就是移除p節(jié)點
last = trail;
// unlink的結(jié)果春缕,當然鏈表未滿霍骄,那么需要喚醒notFull條件
if (count.getAndDecrement() == capacity)
notFull.signal();
}