java 中如何声明线程安全的集合 set, map 和list【转】

线程安全的集合

引用自 http://blog.sina.com.cn/s/blog_508938e10102v1ig.html

//make thread-safe list
  List MyStrList = Collections.synchronizedList(new ArrayList());
  MyStrList.add("123");
  MyStrList.add("abc");
  //make thread-safe hashset
  Set set=Collections.synchronizedSet(new HashSet());
  set.add(123);
  set.add(456);
  //make thread-safe hashmap
  Map map=Collections.synchronizedMap(new HashMap());
  map.put(1, "thb");
  map.put(12, "bill tang");

CopyOnWriteArrayList与Collections.synchronizedList的性能对比

http://blog.csdn.net/zljjava/article/details/48139465

原文地址:https://www.cnblogs.com/whatlonelytear/p/5638082.html