小弟最近在翻看JDK的源码但是奈何基础太差有一处始终想不通。希望大家帮忙解答下共同进步。
ConcurrentLinkedQueue的源码中的offer方法
(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
}
)
我的理解是 既然t = p; 那么对p的操作应该等同与对t的操作,那么将newNode设置为p.next不久等同于对t也设置next属性么, 为什么p!=t呢?
侃侃尔雅
ibeautiful
相关分类