JUC集合安全-Map

ITYWF發表於2020-11-18
  • 和上面兩個不同的是: new ConcurrentHashMap<>();分段鎖
  • 或者直接new Hashtable<>();
    */
    public class Map_Security {
    public static void main(String[] args) {
    //Map<String,String> map = new HashMap<>();
    //Map<String,String> map = new Hashtable<>();
    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();
    }
    }
    }

相關文章