[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 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 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多线程,java,开发语言,后端)