ConcurrentLinkedQueue代码说明

http://www.java2s.com/Open-Source/Java-Open-Source-Library/7-JDK/java/java/util/concurrent/ConcurrentLinkedQueue.java.htm

以上是ConcurrentLinkedQueue的源代码。 我无法理解一个条件。

条件(p == q)将如何来自offer方法的以下代码段

public boolean offer(E e) { checkNotNull(e); final Node newNode = new Node(e); for (Node t = tail, p = t;;) { Node 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; } } 

还有作者的意思是“我们已经脱离了名单”

ConcurrentLinkedQueue允许在遍历内部列表时同时修改内部列表。 这意味着您正在查看的节点可能已同时删除。 为了检测这种情况,删除节点的下一个指针被改变为指向它自己。 有关详细信息,请查看updateHead (L302)。

条件询问“当前节点与下一个节点是否相同?”

如果是这样,你已经从列表中删除了(文档中的文档。)

步骤的基本概要是:

  1. 为提供的数据创建一个新节点。
  2. 遍历列表以查找最后一个节点
  3. 插入新节点作为新尾部。

if语句的其他部分是处理并发修改问题。

为了更好地理解发生了什么,请阅读Node.casTail()和casNext()