1)问题1
package org.example.testChm2;
import com.google.common.collect.Maps;
import java.util.Map;
/**
* @author jianan
* @date 2021/7/2 10:45:06
*/
public class TestChm2 {
public static Map<String, String> testMap = Maps.newConcurrentMap();
public static void main(String[] args) {
System.out.println("AaAa".hashCode() + ":" + "BBBB".hashCode()); // 2031744:2031744
get1("test");
}
public static String get1(String key) {
return testMap.computeIfAbsent("AaAa", TestChm2::get2);
}
public static String get2(String key) {
// 死循环代码(2次操作的是同一个Map)
// BBBB与AaAa的HashCode相同,造成死循环
// return testMap.computeIfAbsent("BBBB", k -> {
// System.out.println("here------------------");
// return "test";
// });
// 没有问题的代码
String v = testMap.get("BBBB");
if (v == null) {
System.out.println("here------------------");
return "test";
}
return v;
}
}
/*
2031744:2031744
Exception in thread "main" java.lang.IllegalStateException: Recursive update
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1760)
at org.example.testChm2.TestChm2.get2(TestChm2.java:28)
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705)
at org.example.testChm2.TestChm2.get1(TestChm2.java:23)
at org.example.testChm2.TestChm2.main(TestChm2.java:19)
here------------------
*/
2)问题2 // 虽然是单线程环境下,但是遍历时又修改 ,依然会报错。用chm即可
package org.example.testmap;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
public class Main4 {
public static void main(String[] args) {
Map<Integer, Integer> map = new HashMap<>();
// 使用了这个则不会报错
map = new ConcurrentHashMap<>();
int i = 0;
while (i++ < 1000) {
map.put(i, 1);
for (Integer key : map.keySet()) {
map.put(ThreadLocalRandom.current().nextInt(5), 1);
map.remove(key);
}
}
System.out.println(map.size());
}
}
/*
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.HashMap$HashIterator.nextNode(HashMap.java:1493)
at java.base/java.util.HashMap$KeyIterator.next(HashMap.java:1516)
at org.example.testmap.Main4.main(Main4.java:15)
*/
本文通过两个示例分析了Java中并发编程的问题。问题1探讨了`ConcurrentHashMap`中因键的哈希值相同导致的死循环问题,展示了正确和错误的使用方式。问题2解释了为何在单线程环境下修改HashMap时仍可能抛出`ConcurrentModificationException`,并提出使用`ConcurrentHashMap`避免此类异常。这两个问题强调了在并发环境中正确选择和使用集合类的重要性。

4250

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



