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