JUC-Map

作者: GIT提交不上 | 来源:发表于2020-02-25 19:09 被阅读0次

  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();
        }
    }
}

相关文章

  • JUC-Map

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

网友评论

      本文标题:JUC-Map

      本文链接:https://www.haomeiwen.com/subject/jfcfchtx.html