• ConcurrentHashMap与Hashtable、Collections.synchronizedMap(map)比较


    ConcurrentHashMap是对HashMap的高并发实现

    压力测试方法:

    /*
         * boolean retrieve : 是否获取,true获取和插入,false只插入
         */
        public static void pressureTest(final Map<String, Integer> map, int threshold, boolean retrieve) throws InterruptedException {
            System.out.println("Start pressure testing the map ["+map.getClass()+"] use the threshold ["+threshold+"],retrieve=" + retrieve);
            long totalTime = 0L;
            final int MAX_THRESHOLD = 2500000;
            for(int i = 0; i < 5; i++) {
                long startTime = System.nanoTime();
                ExecutorService executorService = Executors.newFixedThreadPool(threshold);
                
                for(int j = 0; j < threshold; j++) {
                    executorService.execute(new Runnable() {
                        @Override
                        public void run() {
                            for(int i = 0; i < MAX_THRESHOLD; i++) {
                                Integer randomNumber = (int) Math.ceil(Math.random() * 600000);
                                if(retrieve) {
                                    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(threshold * MAX_THRESHOLD + " element insert/retrieved in " + period + " ms.");
                
                totalTime += period;
            }
            
            System.out.println("For the map ["+map.getClass()+"] the average time is " + (totalTime/5) + " ms.");
        }

    main方法:

    public static void main(String[] args) throws InterruptedException {
            /*
             *     比较在多线程中的性能差距
             * Start pressure testing the map [class java.util.Hashtable] use the threshold [5],retrieve=false
                2500000 element insert/retrieved in 1600 ms.
                2500000 element insert/retrieved in 1166 ms.
                2500000 element insert/retrieved in 1126 ms.
                2500000 element insert/retrieved in 1191 ms.
                2500000 element insert/retrieved in 1260 ms.
                For the map [class java.util.Hashtable] the average time is 1268 ms.
                Start pressure testing the map [class java.util.Hashtable] use the threshold [5],retrieve=true
                2500000 element insert/retrieved in 2411 ms.
                2500000 element insert/retrieved in 2412 ms.
                2500000 element insert/retrieved in 2450 ms.
                2500000 element insert/retrieved in 2393 ms.
                2500000 element insert/retrieved in 2470 ms.
                -----------------------------------
                For the map [class java.util.Hashtable] the average time is 2427 ms.
                Start pressure testing the map [class java.util.Collections$SynchronizedMap] use the threshold [5],retrieve=false
                2500000 element insert/retrieved in 1474 ms.
                2500000 element insert/retrieved in 1483 ms.
                2500000 element insert/retrieved in 1468 ms.
                2500000 element insert/retrieved in 1615 ms.
                2500000 element insert/retrieved in 1395 ms.
                For the map [class java.util.Collections$SynchronizedMap] the average time is 1487 ms.
                Start pressure testing the map [class java.util.Collections$SynchronizedMap] use the threshold [5],retrieve=true
                2500000 element insert/retrieved in 2379 ms.
                2500000 element insert/retrieved in 2339 ms.
                2500000 element insert/retrieved in 2393 ms.
                2500000 element insert/retrieved in 2347 ms.
                2500000 element insert/retrieved in 2358 ms.
                For the map [class java.util.Collections$SynchronizedMap] the average time is 2363 ms.
                -----------------------------------
                Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [5],retrieve=false
                2500000 element insert/retrieved in 965 ms.
                2500000 element insert/retrieved in 830 ms.
                2500000 element insert/retrieved in 660 ms.
                2500000 element insert/retrieved in 805 ms.
                2500000 element insert/retrieved in 605 ms.
                For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 773 ms.
                Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [5],retrieve=true
                2500000 element insert/retrieved in 832 ms.
                2500000 element insert/retrieved in 779 ms.
                2500000 element insert/retrieved in 674 ms.
                2500000 element insert/retrieved in 778 ms.
                2500000 element insert/retrieved in 748 ms.
                For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 762 ms.
                end...
                
                可以看出ConcurrentHashMap的效率最高
             */
            pressureTest(new Hashtable<String, Integer>(), 5, false);
            pressureTest(new Hashtable<String, Integer>(), 5, true);
            System.err.println("-----------------------------------");
            
            pressureTest(Collections.synchronizedMap(new HashMap<String, Integer>()), 5, false);
            pressureTest(Collections.synchronizedMap(new HashMap<String, Integer>()), 5, true);
            System.err.println("-----------------------------------");
            
            pressureTest(new ConcurrentHashMap<String, Integer>(), 5, false);
            pressureTest(new ConcurrentHashMap<String, Integer>(), 5, true);
            System.out.println("end...");
            
            /*
             *     比较在单线程中的性能差距
             * Start pressure testing the map [class java.util.Hashtable] use the threshold [1],retrieve=false
                2500000 element insert/retrieved in 1522 ms.
                2500000 element insert/retrieved in 838 ms.
                2500000 element insert/retrieved in 782 ms.
                2500000 element insert/retrieved in 844 ms.
                2500000 element insert/retrieved in 849 ms.
                For the map [class java.util.Hashtable] the average time is 967 ms.
                Start pressure testing the map [class java.util.Hashtable] use the threshold [1],retrieve=true
                2500000 element insert/retrieved in 1306 ms.
                2500000 element insert/retrieved in 1238 ms.
                2500000 element insert/retrieved in 1235 ms.
                2500000 element insert/retrieved in 1168 ms.
                2500000 element insert/retrieved in 1201 ms.
                -----------------------------------
                For the map [class java.util.Hashtable] the average time is 1229 ms.
                Start pressure testing the map [class java.util.Collections$SynchronizedMap] use the threshold [1],retrieve=false
                2500000 element insert/retrieved in 1189 ms.
                2500000 element insert/retrieved in 960 ms.
                2500000 element insert/retrieved in 1144 ms.
                2500000 element insert/retrieved in 1323 ms.
                2500000 element insert/retrieved in 1116 ms.
                For the map [class java.util.Collections$SynchronizedMap] the average time is 1146 ms.
                Start pressure testing the map [class java.util.Collections$SynchronizedMap] use the threshold [1],retrieve=true
                2500000 element insert/retrieved in 1273 ms.
                2500000 element insert/retrieved in 1319 ms.
                2500000 element insert/retrieved in 1000 ms.
                2500000 element insert/retrieved in 1222 ms.
                2500000 element insert/retrieved in 1319 ms.
                For the map [class java.util.Collections$SynchronizedMap] the average time is 1226 ms.-----------------------------------
                
                Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [1],retrieve=false
                2500000 element insert/retrieved in 1030 ms.
                2500000 element insert/retrieved in 1028 ms.
                2500000 element insert/retrieved in 857 ms.
                2500000 element insert/retrieved in 1194 ms.
                2500000 element insert/retrieved in 873 ms.
                For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 996 ms.
                Start pressure testing the map [class java.util.concurrent.ConcurrentHashMap] use the threshold [1],retrieve=true
                2500000 element insert/retrieved in 1292 ms.
                2500000 element insert/retrieved in 1126 ms.
                2500000 element insert/retrieved in 1249 ms.
                2500000 element insert/retrieved in 1176 ms.
                2500000 element insert/retrieved in 1310 ms.
                For the map [class java.util.concurrent.ConcurrentHashMap] the average time is 1230 ms.
                -----------------------------------
                Start pressure testing the map [class java.util.HashMap] use the threshold [1],retrieve=false
                2500000 element insert/retrieved in 918 ms.
                2500000 element insert/retrieved in 912 ms.
                2500000 element insert/retrieved in 1109 ms.
                2500000 element insert/retrieved in 884 ms.
                2500000 element insert/retrieved in 991 ms.
                For the map [class java.util.HashMap] the average time is 962 ms.
                Start pressure testing the map [class java.util.HashMap] use the threshold [1],retrieve=true
                2500000 element insert/retrieved in 1066 ms.
                2500000 element insert/retrieved in 1327 ms.
                2500000 element insert/retrieved in 1103 ms.
                2500000 element insert/retrieved in 926 ms.
                2500000 element insert/retrieved in 1110 ms.
                For the map [class java.util.HashMap] the average time is 1106 ms.
                end...
                
                hashMap是现成非安全的,在单线程操作下性能并没有超出太多
             */
            pressureTest(new Hashtable<String, Integer>(), 1, false);
            pressureTest(new Hashtable<String, Integer>(), 1, true);
            System.err.println("-----------------------------------");
            
            pressureTest(Collections.synchronizedMap(new HashMap<String, Integer>()), 1, false);
            pressureTest(Collections.synchronizedMap(new HashMap<String, Integer>()), 1, true);
            System.err.println("-----------------------------------");
            
            pressureTest(new ConcurrentHashMap<String, Integer>(), 1, false);
            pressureTest(new ConcurrentHashMap<String, Integer>(), 1, true);
            System.err.println("-----------------------------------");
            
            pressureTest(new HashMap<String, Integer>(), 1, false);
            pressureTest(new HashMap<String, Integer>(), 1, true);
            System.out.println("end...");
        }
  • 相关阅读:
    使用 CSS 强制内容不换行
    奇怪的 Object reference not set to an instance of an object
    (String) 和 String.valueOf() 两种字符串转换的区别
    SQL 里面的 UNION 说明
    Flex/AIR 中 Embed 标签的使用
    把 doc、xls、pdf 转换为 swf 的相关工具
    把指定的站点加到 IE 可信站点中
    在 VS 中快速打开文件或类型
    linux配置网卡
    linux配置本地源
  • 原文地址:https://www.cnblogs.com/zheaven/p/13753768.html
Copyright © 2020-2023  润新知