本文主要是介绍[ConcurrentHashMap] 1.computeIfAbsent嵌套使用会造成死循环 2.解决单线程下遍历过程中修改的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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:2031744get1("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 updateat 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.ConcurrentModificationExceptionat 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)*/
这篇关于[ConcurrentHashMap] 1.computeIfAbsent嵌套使用会造成死循环 2.解决单线程下遍历过程中修改的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!