阅读ConcurrentLinkedQueue 源码的过程中发现了奇怪问题。现象及重复步骤如下:
1.执行代码
    ConcurrentLinkedQueue<Integer> cq = new ConcurrentLinkedQueue();
    cq.offer(1);2.进入到offer方法,看334行 if (p.casNext(null, newNode)):
 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)) { // 334行
                    // 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;
        }
    }此时head == tail == p,head的对象如图所示:2.进入casNext方法,casNext方法代码如下:
boolean casNext(Node<E> cmp, Node<E> val) {
            return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
        }
执行了 UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val)后,this为:
此时只有一点奇怪。3.casNext返回后,各变量如图所示:此时就比较奇怪了,其一本来p和casNext里面的this指向同一个对象,出栈后却不一样了,其二本来要设置p的next,结果设置为不是newNode而是它自己,其三head节点忽然就指向了newNode了,没有看到有这样的操作,求大神解释,多谢!