ConcurrentLinkedQueue 代码说明

ConcurrentLinkedQueue Code Explanation(ConcurrentLinkedQueue 代码说明)

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

问题描述

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

以上是ConcurrentLinkedQueue的源码.我无法理解一种情况.

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

 public boolean offer(E e) {checkNotNull(e);最终节点 EnewNode = 新节点<E>(e);for (Node<E>t = tail, p = t;) {节点 Eq = p.下一个;如果(q == null){//p 是最后一个节点if (p.casNext(null, newNode)) {//CAS成功就是线性化点//让 e 成为这个队列的一个元素,//并且让 newNode 变得活跃".if (p != t)//一次跳两个节点casTail(t, newNode);//失败是可以的.返回真;}//CAS 竞赛输给了另一个线程;下次再读}否则如果 (p == q)//我们从列表中掉了下来.如果尾部不变,它//也将不在列表中,在这种情况下我们需要//跳转到头,所有活动节点总是从头开始//可达.否则,新尾巴是更好的选择.p = (t != (t = 尾)) ?t : 头;别的//在两跳后检查尾部更新.p = (p != t && t != (t = 尾)) ?t : q;}}

还有作者所说的我们从名单上掉下来了"是什么意思

解决方案

ConcurrentLinkedQueue 允许在遍历内部列表的同时对其进行并发修改.这意味着您正在查看的节点可能已被同时删除.为了检测这种情况,被移除节点的下一个指针被改变为指向它自己.查看 updateHead (L302) 了解详情.

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

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

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"

解决方案

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 代码说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ConcurrentLinkedQueue 代码说明

基础教程推荐