【发布时间】:2020-04-15 09:28:59
【问题描述】:
在ConcurrentHashMap.putVal()(JDK 版本:11;ConcurrentHashMap.java;第 1010 行)
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || value == null) throw new NullPointerException();
int hash = spread(key.hashCode());
int binCount = 0;
for (Node<K,V>[] tab = table;;) {
...
}
addCount(1L, binCount);
return null;
}
为什么它使用变量tab 来引用表?
同样在ConcurrentHashMap.get()(从第 934 行开始)
public V get(Object key) {
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
int h = spread(key.hashCode());
if ((tab = table) != null && (n = tab.length) > 0 &&
(e = tabAt(tab, (n - 1) & h)) != null) {
if ((eh = e.hash) == h) {
if ((ek = e.key) == key || (ek != null && key.equals(ek)))
return e.val;
}
else if (eh < 0)
return (p = e.find(h, key)) != null ? p.val : null;
while ((e = e.next) != null) {
if (e.hash == h &&
((ek = e.key) == key || (ek != null && key.equals(ek))))
return e.val;
}
}
return null;
}
【问题讨论】:
-
这是在 ConcurrentHashMap 的源代码中还是在其他人提供的示例代码中?
-
这是从哪里来的?请链接到源代码。
-
@NomadMaker 是的,这是在源代码 int JDK (version 11) ConcurrnentHashMap 1014 行
-
@Thomas Kläger 在堆栈帧中使用新引用可以更快地获取表对象?
-
链接到源代码会方便读者。