LinkedBlockingQueue node next is not mutable

Now I am reading the LinkedBlockingQueue code, but I have a question, maybe it is simple, but I can not find the answer, I really need help.

I noticed that Node.next is unstable, for example:

static class Node<E> { E item; LinkedBlockingQueue.Node<E> next; Node(E var1) { this.item = var1; } } 

So how does the widget of a new node (Node.next) become visible to dequeue through another thread?

 private void enqueue(Node<E> node) { // assert putLock.isHeldByCurrentThread(); // assert last.next == null; last = last.next = node; } private E dequeue() { // assert takeLock.isHeldByCurrentThread(); // assert head.item == null; Node<E> h = head; Node<E> first = h.next; h.next = h; // help GC head = first; E x = first.item; first.item = null; return x; } 
+5
source share
1 answer

The enqueue() and dequeue() methods are private and therefore are used ONLY from other methods within the same class, such as peek() , poll() , put() , offer() , etc.

Locking is performed at a higher level, and it is never possible for two threads to access node instance variables at the same time in a race state.

+2
source

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


All Articles