как некоторые люди уже рекомендовали. вы увидите, что порядок обратный от этого ответа.
от вопрос уже есть ответ здесь:
ConcurrentLinkedQueue Code Объяснение 2 ответаВ исходном коде 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?