ConcurrentSkipListMap VS ConcurrentHashMap
ConcurrentSkipListMap 与 ConcurrentHashMap进行比较
package com.dwz.concurrent; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; public class ConcurrentSkipListMapVSConcurrentHashMap { static class Entry{ int threshold;//线程数 long ms;//运行的平均时间 public Entry(int threshold, long ms) { this.threshold = threshold; this.ms = ms; } @Override public String toString() { return "Count:" + threshold + ",ms:" + ms; } } public static void pressureTest(final Map<String, Integer> map, int threshold) throws InterruptedException { System.out.println("Start pressure testing the map ["+map.getClass()+"] use the threshold ["+threshold+"]"); long totalTime = 0L; final int MAX_THRESHOLD = 500000; for(int i = 0; i < 5; i++) { final AtomicInteger counter = new AtomicInteger(0); map.clear(); long startTime = System.nanoTime(); ExecutorService executorService = Executors.newFixedThreadPool(threshold); for(int j = 0; j < threshold; j++) { executorService.execute(() -> { for(int x = 0; x < MAX_THRESHOLD && counter.getAndIncrement() < MAX_THRESHOLD; x++) { Integer randomNumber = (int) Math.ceil(Math.random() * 600000); map.get(String.valueOf(randomNumber)); map.put(String.valueOf(randomNumber), randomNumber); } }); } executorService.shutdown(); executorService.awaitTermination(2, TimeUnit.HOURS); long endTime = System.nanoTime(); long period = (endTime - startTime) / 1000000L; System.out.println(MAX_THRESHOLD + " element insert/retrieved in " + period + " ms."); totalTime += period; } summary.get(map.getClass()).add(new Entry(threshold, (totalTime/5))); System.out.println("For the map ["+map.getClass()+"] the average time is " + (totalTime/5) + " ms."); } private final static Map<Class<?>, List<Entry>> summary = new HashMap<Class<?>, List<Entry>>() { { put(ConcurrentHashMap.class, new ArrayList<>()); put(ConcurrentSkipListMap.class, new ArrayList<>()); } }; public static void main(String[] args) throws InterruptedException { for(int i = 10; i < 100;) { pressureTest(new ConcurrentHashMap<>(), i); pressureTest(new ConcurrentSkipListMap<>(), i); i += 10; } summary.forEach((k, v) -> { System.out.println(k.getSimpleName()); v.forEach(System.out::println); System.out.println("============================="); }); } }
代码执行结果:
Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [10] 500000 element insert/retrieved in 480 ms. 500000 element insert/retrieved in 167 ms. 500000 element insert/retrieved in 315 ms. 500000 element insert/retrieved in 107 ms. 500000 element insert/retrieved in 130 ms. For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 239 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentSkipListMap] use the threshold [10] 500000 element insert/retrieved in 783 ms. 500000 element insert/retrieved in 641 ms. 500000 element insert/retrieved in 712 ms. 500000 element insert/retrieved in 643 ms. 500000 element insert/retrieved in 558 ms. For the map [class java.util.concurrent.ConcurrentSkipListMap] the average time is 667 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [20] 500000 element insert/retrieved in 619 ms. 500000 element insert/retrieved in 120 ms. 500000 element insert/retrieved in 126 ms. 500000 element insert/retrieved in 138 ms. 500000 element insert/retrieved in 129 ms. For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 226 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentSkipListMap] use the threshold [20] 500000 element insert/retrieved in 520 ms. 500000 element insert/retrieved in 638 ms. 500000 element insert/retrieved in 535 ms. 500000 element insert/retrieved in 537 ms. 500000 element insert/retrieved in 539 ms. For the map [class java.util.concurrent.ConcurrentSkipListMap] the average time is 553 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [30] 500000 element insert/retrieved in 279 ms. 500000 element insert/retrieved in 121 ms. 500000 element insert/retrieved in 127 ms. 500000 element insert/retrieved in 131 ms. 500000 element insert/retrieved in 122 ms. For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 156 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentSkipListMap] use the threshold [30] 500000 element insert/retrieved in 513 ms. 500000 element insert/retrieved in 537 ms. 500000 element insert/retrieved in 509 ms. 500000 element insert/retrieved in 548 ms. 500000 element insert/retrieved in 512 ms. For the map [class java.util.concurrent.ConcurrentSkipListMap] the average time is 523 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [40] 500000 element insert/retrieved in 182 ms. 500000 element insert/retrieved in 124 ms. 500000 element insert/retrieved in 129 ms. 500000 element insert/retrieved in 126 ms. 500000 element insert/retrieved in 122 ms. For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 136 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentSkipListMap] use the threshold [40] 500000 element insert/retrieved in 526 ms. 500000 element insert/retrieved in 558 ms. 500000 element insert/retrieved in 524 ms. 500000 element insert/retrieved in 598 ms. 500000 element insert/retrieved in 557 ms. For the map [class java.util.concurrent.ConcurrentSkipListMap] the average time is 552 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [50] 500000 element insert/retrieved in 166 ms. 500000 element insert/retrieved in 128 ms. 500000 element insert/retrieved in 143 ms. 500000 element insert/retrieved in 132 ms. 500000 element insert/retrieved in 120 ms. For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 137 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentSkipListMap] use the threshold [50] 500000 element insert/retrieved in 485 ms. 500000 element insert/retrieved in 535 ms. 500000 element insert/retrieved in 503 ms. 500000 element insert/retrieved in 549 ms. 500000 element insert/retrieved in 589 ms. For the map [class java.util.concurrent.ConcurrentSkipListMap] the average time is 532 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [60] 500000 element insert/retrieved in 224 ms. 500000 element insert/retrieved in 137 ms. 500000 element insert/retrieved in 151 ms. 500000 element insert/retrieved in 143 ms. 500000 element insert/retrieved in 134 ms. For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 157 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentSkipListMap] use the threshold [60] 500000 element insert/retrieved in 514 ms. 500000 element insert/retrieved in 516 ms. 500000 element insert/retrieved in 502 ms. 500000 element insert/retrieved in 493 ms. 500000 element insert/retrieved in 587 ms. For the map [class java.util.concurrent.ConcurrentSkipListMap] the average time is 522 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [70] 500000 element insert/retrieved in 190 ms. 500000 element insert/retrieved in 136 ms. 500000 element insert/retrieved in 142 ms. 500000 element insert/retrieved in 137 ms. 500000 element insert/retrieved in 132 ms. For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 147 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentSkipListMap] use the threshold [70] 500000 element insert/retrieved in 506 ms. 500000 element insert/retrieved in 498 ms. 500000 element insert/retrieved in 515 ms. 500000 element insert/retrieved in 532 ms. 500000 element insert/retrieved in 510 ms. For the map [class java.util.concurrent.ConcurrentSkipListMap] the average time is 512 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [80] 500000 element insert/retrieved in 258 ms. 500000 element insert/retrieved in 146 ms. 500000 element insert/retrieved in 133 ms. 500000 element insert/retrieved in 150 ms. 500000 element insert/retrieved in 149 ms. For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 167 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentSkipListMap] use the threshold [80] 500000 element insert/retrieved in 593 ms. 500000 element insert/retrieved in 529 ms. 500000 element insert/retrieved in 548 ms. 500000 element insert/retrieved in 546 ms. 500000 element insert/retrieved in 543 ms. For the map [class java.util.concurrent.ConcurrentSkipListMap] the average time is 551 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [90] 500000 element insert/retrieved in 221 ms. 500000 element insert/retrieved in 124 ms. 500000 element insert/retrieved in 136 ms. 500000 element insert/retrieved in 132 ms. 500000 element insert/retrieved in 136 ms. For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 149 ms. Start pressure testing the map [class java.util.concurrent.ConcurrentSkipListMap] use the threshold [90] 500000 element insert/retrieved in 589 ms. 500000 element insert/retrieved in 514 ms. 500000 element insert/retrieved in 562 ms. 500000 element insert/retrieved in 515 ms. 500000 element insert/retrieved in 559 ms. For the map [class java.util.concurrent.ConcurrentSkipListMap] the average time is 547 ms. ConcurrentHashMap Count:10,ms:239 Count:20,ms:226 Count:30,ms:156 Count:40,ms:136 Count:50,ms:137 Count:60,ms:157 Count:70,ms:147 Count:80,ms:167 Count:90,ms:149 ============================= ConcurrentSkipListMap Count:10,ms:667 Count:20,ms:553 Count:30,ms:523 Count:40,ms:552 Count:50,ms:532 Count:60,ms:522 Count:70,ms:512 Count:80,ms:551 Count:90,ms:547 =============================
结果表明:
ConcurrentHashMap确实比ConcurrentSkipListMap性能优越