首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ConcurrentLinkedQueue代码解释

ConcurrentLinkedQueue代码解释
EN

Stack Overflow用户
提问于 2013-09-09 10:36:53
回答 1查看 1.3K关注 0票数 7

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

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

条件(p == q)将如何在下面提供的代码片段中出现

代码语言:javascript
复制
  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;
        }
    }

此外,作者所说的“我们已经从名单上掉了”是什么意思?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-09 11:51:06

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18696343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档