线程安全解决方案
List
ArrayList线程不安全
Vector线程安全,方法加上了sychornized
List synList = Collections.synchronizedList(new ArrayList<>());
List list = new CopyOnWriteArrayList<>();
Set
HashSet不安全
Set集合怎么实现线程安全?
方案一:
和list一样,使用Colletcions这个工具类syn方法类创建个线程安全的set.
Set synSet = Collections.synchronizedSet(new HashSet<>());
方案二:
使用JUC包里面的CopyOnWriteArraySet
Set copySet = new CopyOnWriteArraySet<>();