偶然间看到了这个bug,记录一下
Bug信息
- 版本 JDK1.8(目前测试到8u261仍然有这个问题)(是不是有且仅有这个版本还不定),至少可以肯定的是在1.9已经修复
- 场景 ConcurrentHashMap 中computeIfAbsent 的再嵌套computeIfAbsent/putIfAbsent
- 现象 线程死锁
代码
public class CHMTest {
public static void main(String[] args) {
normal();
bug();
}
private static void bug(){
System.out.println("begin bug code ...");
Map<String, Integer> map = new ConcurrentHashMap<>(16);
System.out.println(map.size());
String hash1 = "AaAa";
String hash2 = "BBBB";
//hash值,相同产生死锁
System.out.println("hash1:"+hash1.hashCode()+",hash1:"+hash2.hashCode());
map.computeIfAbsent(hash1, key -> {
map.putIfAbsent(hash2,1);
return 1;
});
System.out.println(map.size());
System.out.println("bug code is exec here?");
}
private static void normal(){
System.out.println("begin normal code ...");
Map<String, Integer> map = new ConcurrentHashMap<>(16);
System.out.println(map.size());
String hash1 = "AaAa";
String hash2 = "BBBB2";
System.out.println("hash1:"+hash1.hashCode()+",hash1:"+hash2.hashCode());
map.computeIfAbsent(hash1, key -> {
map.putIfAbsent(hash2,1);
return 1;
});
System.out.println(map.size());
System.out.println("begin normal code finished");
}
}
执行上面的代码,bug方法块将死锁.
在computeIfAbsent 中嵌套computeIfAbsent 或者putIfAbsent时,
当两个key对应的hash值相同发生死锁
本文记录了在JDK1.8中,ConcurrentHashMap的computeIfAbsent方法嵌套使用时出现的线程死锁bug。该问题在JDK1.9已修复,涉及相同key的hash值冲突。示例代码展示了引发死锁的情况。
5632

被折叠的 条评论
为什么被折叠?



