【发布时间】:2015-07-29 15:02:11
【问题描述】:
我的问题是关于文档中的一句话:
与大多数方法不同,对方法 equals 的调用不会被筛选:由于遍历速度并不重要,我们不妨帮助预热相关代码和访问。
上面这句话我看不懂。具体来说,“未筛选”是什么意思?为什么我们可以“预热相关代码”?
scanAndLockForPut
private HashEntry<K,V> scanAndLockForPut(K key, int hash, V value) {
HashEntry<K,V> first = entryForHash(this, hash);
HashEntry<K,V> e = first;
HashEntry<K,V> node = null;
int retries = -1; // negative while locating node
while (!tryLock()) {
HashEntry<K,V> f; // to recheck first below
if (retries < 0) {
if (e == null) {
if (node == null) // speculatively create node
node = new HashEntry<K,V>(hash, key, value, null);
retries = 0;
}
else if (key.equals(e.key))
retries = 0;
else
e = e.next;
}
else if (++retries > MAX_SCAN_RETRIES) {
lock();
break;
}
else if ((retries & 1) == 0 &&
(f = entryForHash(this, hash)) != first) {
e = first = f; // re-traverse if entry changed
retries = -1;
}
}
return node;
【问题讨论】:
-
我认为由于锁定保持时间很短,它会在等待而不是锁定时尝试预热缓存,并且可能会发生上下文切换。
标签: java concurrency java.util.concurrent concurrenthashmap