[java多執行緒]高併發List與Map
阿新 • • 發佈:2019-06-10
public static List<ThreadInfo> threads = new ArrayList<>();// 執行緒不安全 public static List<ThreadInfo> threads = Collections.synchronizedList(new ArrayList<>());// 執行緒安全 public static List<ThreadInfo> threadCopyOnWriteArrayList = new CopyOnWriteArrayList<>();// 執行緒安全 public static ThreadLocal<List<ThreadInfo>> threadList = new ThreadLocal<List<ThreadInfo>>();// 執行緒安全 // Vector也可以考慮?no // public static Map<Long, Map<String, StatsReport>> threadReportMap = new HashMap<>();// 單執行緒 public static Map<Long, Map<String, StatsReport>> threadReportMap = new ConcurrentHashMap<>();// 多執行緒 // ThreadUtils.threads.forEach(threadInfo -> {// 單執行緒 ThreadUtils.threads.parallelStream().forEach(threadInfo -> {// 並行 ... });
- arraylist執行緒不安全
- https://www.cnblogs.com/yan456jie/p/5369366.html 很好的比較
CopyOnWriteArrayList的寫操作效能較差,而多執行緒的讀操作效能較好。 而Collections.synchronizedList的寫操作效能比CopyOnWriteArrayList在多執行緒操作的情況下要好很多, 而讀操作因為是採用了synchronized關鍵字的方式,其讀操作效能並不如CopyOnWriteArrayList。
- 找到答案:(很難找)
- https://www.cnblogs.com/hongdada/p/10931891.html
- Collections.synchronizedList的add、get等都加了同步,因此可以直接用。但iterator沒有。 -不用HashTable,用:ConcurrentHashM