JDK-8299339 : HashMap merge and compute methods can cause odd resizing pathologies

贴一下去年发现的 Bug,嘿嘿。

导致 Bug 的示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Main {
public static void main(String[] args) {
Map<Integer, Integer> map = new HashMap<>(2);

map.merge(1, 1, Integer::sum);
map.merge(2, 1, Integer::sum);

map.forEach((k, v) -> {
map.merge(k, -1, Integer::sum);
System.out.println(k);
});
}
}

Java Bug DataBase 链接,里面比较详细的讨论了发生的问题,由于当时急着发出去,我的评论有点乱,而且是中文翻译为英文的,有点拉。

JDK-8299339 : HashMap merge and compute methods can cause odd resizing pathologies

https://ligh0x74.github.io/2023/10/09/JDK-8299339 HashMap merge and compute methods can cause odd resizing pathologies/

作者

Ligh0x74

发布于

2023-10-09

更新于

2023-10-09

许可协议

评论