【发布时间】:2017-10-19 12:56:20
【问题描述】:
查看java.util.ConcurrentHashMap.putVal(),我遇到了一些检查数组中是否有任何对象(节点)具有hashcode() 作为-ve 值的条件。这段代码,例如:
for (Node<K,V>[] tab = table;;) {
Node<K,V> f; int n, i, fh;
if (tab == null || (n = tab.length) == 0)
tab = initTable();
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
else if ((fh = f.hash) == MOVED) // THIS HERE???
tab = helpTransfer(tab, f);
请注意,它检查fh==MOVED 和MOVED 是否为-1,那么CHM 中哪些对象(节点)的hashcode() 为负数?
进一步阅读文档,它说除了真实节点之外,可能存在 3 种类型的节点(bin 中的第一个),它们的 hashcode() 可能在:
static final int MOVED = -1; // hash for forwarding nodes
static final int TREEBIN = -2; // hash for roots of trees
static final int RESERVED = -3; // hash for transient reservations
我可以理解首先是在调整地图大小时作为临时参考,但我似乎无法理解 -2 和 -3 hashcode() 的用法。帮忙?
【问题讨论】:
标签: java hashmap java.util.concurrent