LinkedBlockingQueue 节点的下一个不是易失性的



我现在正在阅读 LinkedBlockingQueue 代码,但我有一个问题,也许很简单,但我找不到答案,真的需要帮助。

我注意到 Node.next 不是易失性的,如下所示:

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

那么,新节点(Node.next(的排队如何通过另一个线程对取消排队可见?

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

方法enqueue()dequeue()是私有的,因此只能从同一类中的其他方法调用,例如peek()poll()put()offer()等。

锁定是在更高级别强制执行的,在争用条件下,两个线程永远没有机会同时访问节点的实例变量。

相关内容

最新更新