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