OpenJDK 的 LinkedBlockingQueue 实现:Node 类和 GC
我对 OpenJDK 的 LinkedBlockingQueue 实现(在 java.util.concurrent 中)中的 Node 类的结构有点困惑.
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; }
}
具体来说,我对 next 的第二个选择感到困惑(这个节点,意思是后继者是 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;
}
所以我们已经移除了当前的 head,并且我们将它的 next 设置为本身来帮助 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#浮动的垃圾
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
相关文章