ConcurrentHashMap
- 
Map不安全  
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16  
/**
 * The maximum capacity, used if a higher value is implicitly specified
 * by either of the constructors with arguments.
 * MUST be a power of two <= 1<<30.
 */
static final int MAXIMUM_CAPACITY = 1 << 30;
/**
 * The load factor used when none specified in constructor.
 */
static final float DEFAULT_LOAD_FACTOR = 0.75f;  //默认的加载因子
//java.util.ConcurrentModificationException 并发修改异常
public class Test04 {
    public static void main(String[] args) {
        //map是这伴用的吗?不是,工作中不用 Hashmap
        //就认等价于什么?HashMap<String, String> map = new HashMap<>(16,0.75)
        HashMap<String, String> map = new HashMap<>();
        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                map.put(Thread.currentThread().getName(),UUID.randomUUID().toString().substring(0,5));
                System.out.println(map);
            },String.valueOf(i)).start();
        }
    }
}
//Exception in thread "3" Exception in thread "0" Exception in thread "1" Exception in thread "2" Exception in thread "14" Exception in thread "13" Exception in thread "17" Exception in thread "19" java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1463)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1461)
	at java.util.AbstractMap.toString(AbstractMap.java:531)
	at java.lang.String.valueOf(String.java:2981)
	at java.io.PrintStream.println(PrintStream.java:821)
	at com.saxon.demo.Test04.lambda$main$0(Test04.java:15)
- 
解决方案
- 使用Collections下的Collections.synchronizedMap(new HashMap<>());方法处理并发
- JUC下的ConcurrentHashMap方法
 
//java.util.ConcurrentModificationException 并发修改异常
public class Test04 {
    public static void main(String[] args) {
        //map是这伴用的吗?不是,工作中不用 Hashmap
        //就认等价于什么?HashMap<String, String> map = new HashMap<>(16,0.75)
        // HashMap<String, String> map = new HashMap<>();
        /**
         * 解决方案
         * 1. Map<String, String> map = Collections.synchronizedMap(new HashMap<>());
         * 2. Map<String, String> map = new ConcurrentHashMap<>();
         */
        Map<String, String> map = new ConcurrentHashMap<>();
        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                map.put(Thread.currentThread().getName(),UUID.randomUUID().toString().substring(0,5));
                System.out.println(map);
            },String.valueOf(i)).start();
        }
    }
}