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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM