zoukankan      html  css  js  c++  java
  • 如何创建一个线程安全的Map?

    1,使用普通的旧的Hashtable

      HashMap允许null作为key,而Hashtable不可以

    2,使用Collections中同步化的包装方法synchronizedMap

    3,使用concurrent包下的ConcurrentHashMap 

    //Hashtable Example Code
    Map<String, Integer> threadSafeMap = new Hashtable<String, Integer>();
    //synchronizedMap Example Code.
    threadSafeMap = Collections.synchronizedMap(new HashMap<String, Integer>());
    //ConcurrentHashMap Example Code
    threadSafeMap = new ConcurrentHashMap<String, Integer>();
    threadSafeMap .put("Key1", 123)

    ConcurrentHashMap 性能最好

     

  • 相关阅读:
    第四次作业
    第三次
    第十次作业
    第九次作业
    第八次作业
    10.29第七次
    15
    14
    13 this
    12 电视机
  • 原文地址:https://www.cnblogs.com/once/p/3657485.html
Copyright © 2011-2022 走看看