java-OpenJDK的LinkedBlockingQueue实现:节点类和GC
作者:互联网
这个问题已经在这里有了答案: > Strange code in java.util.concurrent.LinkedBlockingQueue 4个
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; }
}
具体来说,我对下一个第二选择感到困惑(“此节点,意味着后继者是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;
}
因此,我们已删除了当前标题,并将其下一个设置为“ help GC”.
这如何帮助GC?对GC有多大帮助?
解决方法:
我问了代码作者Doug Lea教授,他说这可以减少
GC留下漂浮垃圾的机会.
一些有关浮动垃圾的有用资源:
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
标签:java,concurrency,garbage-collection,queue,java-util-concurrent 来源: https://codeday.me/bug/20191011/1891115.html