运行时跟随代码,永远不会终止并陷入无限循环。
我不确定它卡在哪里。
有趣的是,当我将 AaAa
更改为 AAAA
时,一切都按预期正常工作。
public class Test {
public static void main(String[] args) {
Map<String, Integer> map = new ConcurrentHashMap<>(16);
map.computeIfAbsent(
"AaAa",
key -> {
return map.computeIfAbsent(
"BBBB",
key2 -> 42);
}
);
}
}
谁能帮我理解这种行为。
最佳答案
"AaAa"
和 "BBBB"
具有相同的 hashCode()
- 2031744。
因此,这两个键都映射到 Map 的同一个 bin。
外部 map.computeIfAbsent
锁定那个 bin,而内部 map.computeIfAbsent
试图在释放锁之前锁定它 - 因此出现死锁。
关于java - ConcurrentHashMap 中的死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43861945/