【发布时间】:2018-06-27 09:25:48
【问题描述】:
在ConcurrentLinkedQueue的源码中,offer方法中:
public boolean offer(E e) {
checkNotNull(e);
final Node<E> newNode = new Node<E>(e);
for (Node<E> t = tail, p = t;;) {
Node<E> q = p.next;
if (q == null) {
// p is last node
if (p.casNext(null, newNode)) {
// Successful CAS is the linearization point
// for e to become an element of this queue,
// and for newNode to become "live".
if (p != t) // hop two nodes at a time
casTail(t, newNode); // Failure is OK.
return true;
}
// Lost CAS race to another thread; re-read next
}
else if (p == q)
// We have fallen off list. If tail is unchanged, it
// will also be off-list, in which case we need to
// jump to head, from which all live nodes are always
// reachable. Else the new tail is a better bet.
p = (t != (t = tail)) ? t : head;
else
// Check for tail updates after two hops.
p = (p != t && t != (t = tail)) ? t : q;
}
}
在第 352 行,有这样的条件:
p = (p != t && t != (t = tail)) ? t : q;
我知道代码是把p放在后面,但是为什么要用这么复杂的代码呢? (p != t && t != (t = tail))是什么意思? t!=(t=tail)) 和 t!=t 有什么区别?它应该总是假的吗?
有什么资料可以把ConcurrentLinkedQueue解释清楚吗?
【问题讨论】:
-
t和tail的类型有哪些? -
Tail和t在源代码中是Node<E>类型。
标签: java concurrency