ConcurrentLinkedQueue Parameter Explanation

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 for ConcurrentLinkedQueue. I can not understand one condition.

How the condition (p == q) appears in the code snippet from the sentence method below

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

as well as the fact that the author means "We have fallen off the list"

+6
source share
2 answers

ConcurrentLinkedQueue allows you to simultaneously modify the internal list when moving it. This means that the node you are viewing can be deleted at the same time. To detect such situations, the next pointer of the deleted node changes to a point for itself. See updateHead (L302) for details.

+5
source

The condition asks the question "Is the current node the same as the next node?"

If so, you have dropped the list (documentation in line.)

The basic plan of steps:

  • create a new node for the proposed data.
  • go through the list to find the last node
  • insert the new node as the new tail.

Other parts of the if statement handle problems with simultaneous modification.

To better understand what's going on, read Node.casTail () and casNext ()

+2
source

Source: https://habr.com/ru/post/953471/


All Articles