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 -> {// 並行 ... });
CopyOnWriteArrayList的寫操做性能較差,而多線程的讀操做性能較好。 而Collections.synchronizedList的寫操做性能比CopyOnWriteArrayList在多線程操做的狀況下要好不少, 而讀操做由於是採用了synchronized關鍵字的方式,其讀操做性能並不如CopyOnWriteArrayList。html