// 建立執行緒安全的ListList<Object> list = Collections.synchronizedList(new ArrayList<>());// 建立執行緒安全的MapMap<String, Object> map = Collections.synchronizedMap(new HashMap<String, Object>());
比較實用!!建立執行緒安全的集合還可以使用以下的方法:
//mapMap<String, Object> map = new ConcurrentHashMap<String, Object>();ConcurrentSkipListMap<String, Object> skipListMap = new ConcurrentSkipListMap<>();//listList<?> list = new Vector<>();CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList();CopyOnWriteArraySet copyOnWriteArraySet = new CopyOnWriteArraySet();
最新評論