package org.wutest;import java.util.Collections;import java.util.HashMap;import java.util.Hashtable;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;/** * \* Created with IntelliJ IDEA. * \* User: wugong.jie * \* Date: 2018/2/1 10:24 * \* To change this template use File | Settings | File Templates. * \* Description: * \ */public class ConcurrentDemo { public final static int THREAD_POOL_SIZE = 5; public static MapcrunchifyHashTableObject = null; public static Map crunchifySynchronizedMapObject = null; public static Map crunchifyConcurrentHashMapObject = null; public static void main(String[] args) throws InterruptedException { // Test with Hashtable Object crunchifyHashTableObject = new Hashtable<>(); crunchifyPerformTest(crunchifyHashTableObject); // Test with synchronizedMap Object crunchifySynchronizedMapObject = Collections.synchronizedMap(new HashMap ()); crunchifyPerformTest(crunchifySynchronizedMapObject); // Test with ConcurrentHashMap Object crunchifyConcurrentHashMapObject = new ConcurrentHashMap<>(); crunchifyPerformTest(crunchifyConcurrentHashMapObject); } public static void crunchifyPerformTest(final Map crunchifyThreads) throws InterruptedException { System.out.println("Test started for: " + crunchifyThreads.getClass()); long averageTime = 0; for (int i = 0; i < 5; i++) { long startTime = System.nanoTime(); ExecutorService crunchifyExServer = Executors.newFixedThreadPool(THREAD_POOL_SIZE); for (int j = 0; j < THREAD_POOL_SIZE; j++) { crunchifyExServer.execute(new Runnable() { @SuppressWarnings("unused") @Override public void run() { for (int i = 0; i < 500000; i++) { Integer crunchifyRandomNumber = (int) Math.ceil(Math.random() * 550000); // Retrieve value. We are not using it anywhere Integer crunchifyValue = crunchifyThreads.get(String.valueOf(crunchifyRandomNumber)); // Put value crunchifyThreads.put(String.valueOf(crunchifyRandomNumber), crunchifyRandomNumber); } } }); } // Make sure executor stops crunchifyExServer.shutdown(); // Blocks until all tasks have completed execution after a shutdown request crunchifyExServer.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); long entTime = System.nanoTime(); long totalTime = (entTime - startTime) / 1000000L; averageTime += totalTime; System.out.println("2500K entried added/retrieved in " + totalTime + " ms"); } System.out.println("For " + crunchifyThreads.getClass() + " the average time is " + averageTime / 5 + " ms\n"); }}
结果:
"C:\Program Files\Java\jdk1.8.0_101\bin\java" "-javaagent:D:\softinstall\IntelliJ IDEA 2017.1\lib\idea_rt.jar=53631:D:\softinstall\IntelliJ IDEA 2017.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_101\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\rt.jar;D:\project\home_ssm_02\home_ssm\target\test-classes;D:\project\home_ssm_02\home_ssm\target\classes;D:\mavn-repository\junit\junit\4.11\junit-4.11.jar;D:\mavn-repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\mavn-repository\org\springframework\spring-core\4.0.2.RELEASE\spring-core-4.0.2.RELEASE.jar;D:\mavn-repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;D:\mavn-repository\org\springframework\spring-web\4.0.2.RELEASE\spring-web-4.0.2.RELEASE.jar;D:\mavn-repository\org\springframework\spring-beans\4.0.2.RELEASE\spring-beans-4.0.2.RELEASE.jar;D:\mavn-repository\org\springframework\spring-context\4.0.2.RELEASE\spring-context-4.0.2.RELEASE.jar;D:\mavn-repository\org\springframework\spring-oxm\4.0.2.RELEASE\spring-oxm-4.0.2.RELEASE.jar;D:\mavn-repository\org\springframework\spring-tx\4.0.2.RELEASE\spring-tx-4.0.2.RELEASE.jar;D:\mavn-repository\org\springframework\spring-jdbc\4.0.2.RELEASE\spring-jdbc-4.0.2.RELEASE.jar;D:\mavn-repository\org\springframework\spring-webmvc\4.0.2.RELEASE\spring-webmvc-4.0.2.RELEASE.jar;D:\mavn-repository\org\springframework\spring-expression\4.0.2.RELEASE\spring-expression-4.0.2.RELEASE.jar;D:\mavn-repository\org\springframework\spring-aop\4.0.2.RELEASE\spring-aop-4.0.2.RELEASE.jar;D:\mavn-repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\mavn-repository\org\aspectj\aspectjweaver\1.8.13\aspectjweaver-1.8.13.jar;D:\mavn-repository\org\springframework\spring-context-support\4.0.2.RELEASE\spring-context-support-4.0.2.RELEASE.jar;D:\mavn-repository\org\springframework\spring-test\4.0.2.RELEASE\spring-test-4.0.2.RELEASE.jar;D:\mavn-repository\org\mybatis\mybatis\3.2.6\mybatis-3.2.6.jar;D:\mavn-repository\org\mybatis\mybatis-spring\1.2.2\mybatis-spring-1.2.2.jar;D:\mavn-repository\javax\javaee-api\7.0\javaee-api-7.0.jar;D:\mavn-repository\com\sun\mail\javax.mail\1.5.0\javax.mail-1.5.0.jar;D:\mavn-repository\javax\activation\activation\1.1\activation-1.1.jar;D:\mavn-repository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar;D:\mavn-repository\commons-dbcp\commons-dbcp\1.2.2\commons-dbcp-1.2.2.jar;D:\mavn-repository\commons-pool\commons-pool\1.3\commons-pool-1.3.jar;D:\mavn-repository\jstl\jstl\1.2\jstl-1.2.jar;D:\mavn-repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\mavn-repository\com\alibaba\fastjson\1.2.38\fastjson-1.2.38.jar;D:\mavn-repository\org\slf4j\slf4j-api\1.7.7\slf4j-api-1.7.7.jar;D:\mavn-repository\org\slf4j\slf4j-log4j12\1.7.7\slf4j-log4j12-1.7.7.jar;D:\mavn-repository\org\codehaus\jackson\jackson-mapper-asl\1.9.13\jackson-mapper-asl-1.9.13.jar;D:\mavn-repository\org\codehaus\jackson\jackson-core-asl\1.9.13\jackson-core-asl-1.9.13.jar;D:\mavn-repository\commons-fileupload\commons-fileupload\1.3.1\commons-fileupload-1.3.1.jar;D:\mavn-repository\commons-io\commons-io\2.4\commons-io-2.4.jar;D:\mavn-repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;D:\mavn-repository\com\fasterxml\jackson\core\jackson-core\2.5.2\jackson-core-2.5.2.jar;D:\mavn-repository\com\fasterxml\jackson\core\jackson-databind\2.5.2\jackson-databind-2.5.2.jar;D:\mavn-repository\com\fasterxml\jackson\core\jackson-annotations\2.5.0\jackson-annotations-2.5.0.jar;D:\mavn-repository\javax\servlet\jstl\1.2\jstl-1.2.jar;D:\mavn-repository\com\google\guava\guava\19.0\guava-19.0.jar;D:\mavn-repository\org\java-websocket\Java-WebSocket\1.3.0\Java-WebSocket-1.3.0.jar;D:\mavn-repository\org\springframework\spring-websocket\4.1.4.RELEASE\spring-websocket-4.1.4.RELEASE.jar;D:\mavn-repository\org\springframework\data\spring-data-redis\1.6.2.RELEASE\spring-data-redis-1.6.2.RELEASE.jar;D:\mavn-repository\redis\clients\jedis\2.8.0\jedis-2.8.0.jar;D:\mavn-repository\org\apache\commons\commons-pool2\2.3\commons-pool2-2.3.jar;D:\mavn-repository\org\mybatis\mybatis-ehcache\1.0.0\mybatis-ehcache-1.0.0.jar;D:\mavn-repository\net\sf\ehcache\ehcache-core\2.0.0\ehcache-core-2.0.0.jar" org.wutest.ConcurrentDemoTest started for: class java.util.Hashtable2500K entried added/retrieved in 2038 ms2500K entried added/retrieved in 1832 ms2500K entried added/retrieved in 1781 ms2500K entried added/retrieved in 1704 ms2500K entried added/retrieved in 1789 msFor class java.util.Hashtable the average time is 1828 msTest started for: class java.util.Collections$SynchronizedMap2500K entried added/retrieved in 1741 ms2500K entried added/retrieved in 1674 ms2500K entried added/retrieved in 1659 ms2500K entried added/retrieved in 1595 ms2500K entried added/retrieved in 1755 msFor class java.util.Collections$SynchronizedMap the average time is 1684 msTest started for: class java.util.concurrent.ConcurrentHashMap2500K entried added/retrieved in 1015 ms2500K entried added/retrieved in 533 ms2500K entried added/retrieved in 527 ms2500K entried added/retrieved in 1629 ms2500K entried added/retrieved in 597 msFor class java.util.concurrent.ConcurrentHashMap the average time is 860 ms