OpenJDK的LinkedBlockingQueue实现:Node类和GC [英] OpenJDK's LinkedBlockingQueue implementation: Node class and GC

查看:114
本文介绍了OpenJDK的LinkedBlockingQueue实现:Node类和GC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a little confused by the structure of the Node class in OpenJDK's implementation of LinkedBlockingQueue (in java.util.concurrent).

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

I've reproduced the description of the node class below:

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

Specifically, I'm confused on the 2nd choice for next ("this Node, meaning successor is head.next").

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

This seems to be directly related to the dequeue method, which looks like:

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。

So we've removed the current head, and we're setting its next to be itself to "help GC".

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

How does this help GC? How helpful is it to GC?

推荐答案

我问代码作者Doug Lea教授,他说它减少了
GC离开浮动垃圾的机会。

I asked Professor Doug Lea, author of the code, and he said that it reduces the chances of GC leaving floating garbage.

浮动垃圾的一些有用资源:

Some useful resources on floating garbage:

http://www.memorymanagement.org/glossary/f.html#floating.garbage

http://java.sun.com/docs/hotspot/gc1.4.2/#4.4.4.%20Floating%20Garbage|outline

http://blog.johantibell.com /2010/04/generational-garbage-collection-and.html

这篇关于OpenJDK的LinkedBlockingQueue实现:Node类和GC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆