多条线程同时处理集合可能出现线程不安全(会报异常)
List
Vector线程安全 底层锁synchronized 效率不高
CopyOnWriteArrayList线程安全 底层锁ReentrantLock 效率稍高
//List list = new ArrayList(); //List list = new Vector(); List list = new CopyOnWriteArrayList(); for (int i = 0; i <100 ; i++) { new Thread(()->{ list.add(UUID.randomUUID().toString().substring(0,5));//取随机值 System.out.println(list.toString()); },""+i).start(); }
Set
同上
线程安全CopyOnWriteArraySet
Map
线程安全ConcurrentHashMap
ConcurrentHashMap kv不能存null
hb