OpenJDK的LinkedBlockingQueue实现:Node类和GC

时间:2012-04-11 12:47:02

标签: java concurrency garbage-collection queue java.util.concurrent

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

我已经复制了以下节点类的描述:

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”)。

这似乎与dequeue方法直接相关,如下所示:

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对它有多大帮助?

1 个答案:

答案 0 :(得分:1)