ConcurrentLinkedQueue代码说明 [英] ConcurrentLinkedQueue Code Explanation

查看:97
本文介绍了ConcurrentLinkedQueue代码说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

The above is the source code of ConcurrentLinkedQueue. I am not able to understand one condition.

条件(p == q)如何出现在下面的代码段中来自offer方法的代码

How the condition (p == q) will come in the below snippet code from offer method

  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;
        }
    }

以及作者的意思是我们有列表

and also what does the author mean by "We have fallen off List"

推荐答案

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

The ConcurrentLinkedQueue allows concurrent modification of the internal list while traversing it. This implies that the node you are looking at could have been removed concurrently. To detect such situations the next pointer of a removed node is changed to point to itself. Look at updateHead (L302) for details.

这篇关于ConcurrentLinkedQueue代码说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆