https://bugs.openjdk.java.net/browse/JDK-8240162
这个 ConcurrentHashMap 的 bug,运行它的用例,会产生一个 bug,就是明明 map 中有映射,但 size 为 0 。
import java.util.concurrent.*;
public class test5 {
public static void main(String[] args) {
final ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
final String key = "key";
final String value = "value";
map.put("anotherKey", "anotherValue");
map.put(key, value);
System.out.println("size=" + map.size());
map.computeIfPresent(key, (aKey, aValue) -> {
map.computeIfPresent(key, (bKey, bValue) -> null);
return null;
});
System.out.println("size=" + map.size());//这句返回 0
map.put(key, value);//这句打断点
System.out.println("size=" + map.size());
}
}
可以发现明明 map 中有映射,但 size 为 0 。
- 为什么没有产生无限递归,看起来会无限递归啊?感觉调用过程好难理解
- 为啥执行完,size 返回错误的值?
运行环境 jdk8