for (Node<E> t= tail, p = t;;) {//自旋 Node<E> q= p.next; //p实际上指向尾节点 if (q == null) { //尾节点下一个为null // p is last node if (p.casNext(null, newNode)) { //CAS插入 // Successful CAS is the linearization point // for e to become an element of this queue, // and for newNode to become "live". if (p != t) //tail之后至少有两个节点才修改tail casTail(t, newNode); // CAS替换尾节点 returntrue; } // Lost CAS race to another thread; re-read next } elseif (p == q)//p节点指向自身,说明p是自链节点 // 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. //如果tail节点被其它线程修改,此时需要从head节点开始向 //后遍历,因为从head开始可达所有的live节点 p = (t != (t = tail)) ? t : head; else // Check for tail updates after two hops. //继续向后查找,如果tail节点变化,重新获取tail。 p = (p != t && t != (t = tail)) ? t : q; } }