【问题标题】:OpenJDK's LinkedBlockingQueue implementation: Node class and GC [duplicate]OpenJDK 的 LinkedBlockingQueue 实现:Node 类和 GC [重复]
【发布时间】:2012-04-23 18:33:02
【问题描述】:

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

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

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

具体来说,我对 next 的第二个选择感到困惑(“这个节点,意思是后继者是 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;
}

所以我们已经移除了当前的 head,并且我们将它的 next 设置为本身来“帮助 GC”。

这对 GC 有什么帮助?对 GC 有多大帮助?

【问题讨论】:

  • 它们可能是相关的,但 util.concurrent 似乎在很多地方都有其他问题中提到的相同类型的代码(即制作成员变量的本地副本),这让我觉得它们是不同的问题。如果它们真的相关,那会很有趣!

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


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多