java.util.Collections類, 該工具類中有一組方法, synchronizedXXX( xxx )可以把xxx集合轉換為線程安全的集合。
static <T> Collection<T> synchronizedCollection(Collection<T> c)
static <T> List<T> synchronizedList(List<T> list)
static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)
static <T> Set<T> synchronizedSet(Set<T> s)
但是,在開發多線程程序時, 基本不使用這一組操作, 在多線程環境中:
● 如果使用List集合就直接使用java.util.concurrent.CopyOnWriteArrayList類。
● 如果使用Set集合,不需要排序,使用java.util.concurrent.CopyOnWriteArraySet類, 如果需要排序,使用java.util.concurrent.ConcurrentSkipListSet集合。