我正在阅读Java LinkedBlockingQueue
源代码,我对put
operation有两个问题。
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.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.
*/
while (count.get() == capacity) {
notFull.await();
}
enqueue(node);
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
}
我不明白:
c + 1 < capacity
时是否notFull.signal()
。为什么不使用c < capacity
?signalNotEmpty
时会发出c == 0
?此外,我也不明白为什么头/尾必须是transient
?
答案 0 :(得分:4)
你的两个问题的答案:
c + 1 < capacity
检查之前,有c = count.getAndIncrement();
的来电。由于计数在之后被提取并被分配到c
,因此c
与<{1}}之间存在 1 的差异。实际计数,因此检查中为+ 1
。c
此时 0 而不是 1 ,请参阅第一个问题的答案。关于head
和tail
transient
:他们不需要在对象序列化时保留,因为他们通过对象的默认构造函数初始化反序列化。