2

我对OpenJDK的LinkedBlockingQueue实现中的Node类的结构(在java.util.concurrent中)有点困惑。OpenJDK的LinkedBlockingQueue实现:节点类和GC

我抄录如下节点类的描述:

static class Node<E> { 
    E item; 

    /** 
    * One of: 
    * - the real successor Node 
    * - this Node, meaning the successor is head.next 
    * - null, meaning there is no successor (this is the last node) 
    */ 
    Node<E> next; 

    Node(E x) { item = x; } 
} 

具体而言,我对接下来的第二选择困惑(“这个节点,这意味着继任者是head.next”)。

这似乎是直接关系到出队的方法,它看起来像:

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

因此,我们已经删除了当前的头,我们设置它的旁边,本身是“帮助GC” 。

这对GC有何帮助?对GC有帮助吗?

+0

可能重复://计算器.com/questions/8819342/strange-code-in-java-util-concurrent-linkedblockingqueue) – artbristol 2012-04-11 16:23:54

+0

它们可能是相关的,但util.concurrent似乎有在其他问题中提到的相同类型的代码在很多地方(即制作一个成员变量的本地副本),这使我认为他们是不同的问题。如果他们实际上是相关的,它会很有趣! – kanak 2012-04-11 23:17:27

回答