JUC-Map

2020-02-25  本文已影响0人  GIT提交不上

  HashMap构造函数:默认容量为16,DEFAULT_LOAD_FACTOR-扩容因子取0.75(泊松分布)。

  /**
    * Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
     */
public HashMap() {
    this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}

  ConcurrentHashMap解决HashMap不安全问题。

/**
 * @author luffy
 **/
public class HashMapDemo {
    public static void main(String[] args){
        //Map<String,String> map = new HashMap<>();
        //Map<String,String> map = Collections.synchronizedMap(new HashMap<>());
        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,10));
                System.out.println(map);
            },String.valueOf(i)).start();
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读