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,用:ConcurrentHashMap