1. HashMap,TreeMap 未進行同步考慮,是線程不安全的。
2. HashTable 和 ConcurrentHashMap 都是線程安全的。區別在於他們對加鎖的範圍不同,HashTable 對整張Hash表進行加鎖,而ConcurrentHashMap將Hash表分爲16桶(segment),每次只對需要的桶進行加鎖。
3. Collections 類提供了synchronizedXxx()方法,可以將指定的集合包裝成線程同步的集合。比如,
List list = Collections.synchronizedList(new ArrayList());
Set set = Collections.synchronizedSet(new HashSet());