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