如何实现线程的HashMap以及性能对比
来源:互联网 发布:昆山数据库培训 编辑:程序博客网 时间:2024/05/21 21:16
对于线程是否是安全的问题,一般是不会用到的,因为在开发中通用的是单线程倒是不用担心线程安全问题。
但是对于要进行高并发的开发,线程安全就必不可少了,这也是面试的拦路虎,在此总结一下前人的经验,进行线程安全的总结,如有不足的地方请指出交流。
借鉴别人的测试代码并完善一下:
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.ConcurrentMap;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicLong;public class CrunchifyConcurrentHashMapVsSynchronizedMap {public final static int THREAD_POOL_SIZE = 10;public final static int THREAD_LOOP_TIME = 100000;public final static String key = "wordcounts" ;public static Map<String, AtomicLong> crunchifyHashTableObject = null;public static Map<String, AtomicLong> crunchifySynchronizedMapObject = null;public static Map<String, AtomicLong> crunchifyConcurrentHashMapObject = null;public static Map<String, AtomicLong> crunchifyHashMapObject = null;public static void main(String[] args) throwsInterruptedException {// 安全性测试mapSecurictyTest();//效率测试mapEfficienceTest();}private static void mapSecurictyTest() throws InterruptedException {// Test with HashMap ObjectcrunchifyHashMapObject = new HashMap<>();threadSecurityTest(crunchifyHashMapObject);// Test with Hashtable ObjectcrunchifyHashTableObject = new Hashtable<>();threadSecurityTest(crunchifyHashTableObject);// Test with synchronizedMap ObjectcrunchifySynchronizedMapObject = Collections.synchronizedMap(new HashMap<String, AtomicLong>());threadSecurityTest(crunchifySynchronizedMapObject);// Test with ConcurrentHashMap ObjectcrunchifyConcurrentHashMapObject = new ConcurrentHashMap<>();threadSecurityTest(crunchifyConcurrentHashMapObject);}private static void mapEfficienceTest() throws InterruptedException {// 效率最高 38msMap<String, Integer> crunchifyHashMapObject = null;//For class java.util.Hashtable the average time is 82 msMap<String, Integer> crunchifyHashTableObject = null;//For class java.util.Collections$SynchronizedMap the average time is 84 msMap<String, Integer> crunchifySynchronizedMapObject = null;//For class java.util.concurrent.ConcurrentHashMap the average time is 58 msMap<String, Integer> crunchifyConcurrentHashMapObject = null;// Test with HashMap ObjectcrunchifyHashMapObject = new HashMap<>();crunchifyPerformTest(crunchifyHashMapObject);// Test with Hashtable ObjectcrunchifyHashTableObject = new Hashtable<>();crunchifyPerformTest(crunchifyHashTableObject);// Test with synchronizedMap ObjectcrunchifySynchronizedMapObject = Collections.synchronizedMap(new HashMap<String, Integer>());crunchifyPerformTest(crunchifySynchronizedMapObject);// Test with ConcurrentHashMap ObjectcrunchifyConcurrentHashMapObject = new ConcurrentHashMap<>();crunchifyPerformTest(crunchifyConcurrentHashMapObject);}public static void threadSecurityTest( final Map<String, AtomicLong> map) throws InterruptedException {System.out.println("Test started for: " + map.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() {@Overridepublic void run() {for (int i = 0; i < THREAD_LOOP_TIME; i++) { AtomicLong number = map.get(key); if (number == null) { AtomicLong newNumber = new AtomicLong(0); number = map.putIfAbsent(key, newNumber); if (number == null) { number = newNumber; } } long incrementValue = number.incrementAndGet(); if ((incrementValue )%500000==00) {System.out.println("value is :" +(incrementValue ));}}}});}// Make sure executor stopscrunchifyExServer.shutdown();// Blocks until all tasks have completed execution after a shutdown requestcrunchifyExServer.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);long entTime = System.nanoTime();long totalTime = (entTime - startTime) / 1000000L;averageTime += totalTime;System.out.println( (i+1 )+"-"+THREAD_POOL_SIZE*THREAD_LOOP_TIME +" entried added/retrieved in " + totalTime + " ms");}System.out.println("For " + map.getClass() + " the average time is " + averageTime / 5 + " ms\n");}public static void crunchifyPerformTest( final Map<String, Integer> 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() {@Overridepublic void run() {for (int i = 0; i < THREAD_LOOP_TIME; i++) { // Retrieve value. We are not using it anywhereInteger crunchifyValue = crunchifyThreads.get(key);// Put valuecrunchifyThreads.put( key, new Integer(1));}}});}// Make sure executor stopscrunchifyExServer.shutdown();// Blocks until all tasks have completed execution after a shutdown requestcrunchifyExServer.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");} }
由打印结果发现 HashMap居然能够累加到500w.难道HashMap也能线程安全吗?
================================== map securicty test start ==================================
Test started for: class java.util.HashMap
value is :500000
value is :1000000
1-1000000 entried added/retrieved in 57 ms
value is :1500000
value is :2000000
2-1000000 entried added/retrieved in 51 ms
value is :2500000
value is :3000000
3-1000000 entried added/retrieved in 38 ms
value is :3500000
value is :4000000
4-1000000 entried added/retrieved in 35 ms
value is :4500000
value is :5000000
5-1000000 entried added/retrieved in 35 ms
For class java.util.HashMap the average time is 43 ms
Test started for: class java.util.Hashtable
value is :500000
value is :1000000
1-1000000 entried added/retrieved in 97 ms
value is :1500000
value is :2000000
2-1000000 entried added/retrieved in 66 ms
value is :2500000
value is :3000000
3-1000000 entried added/retrieved in 67 ms
value is :3500000
value is :4000000
4-1000000 entried added/retrieved in 65 ms
value is :4500000
value is :5000000
5-1000000 entried added/retrieved in 61 ms
For class java.util.Hashtable the average time is 71 ms
Test started for: class java.util.Collections$SynchronizedMap
value is :500000
value is :1000000
1-1000000 entried added/retrieved in 111 ms
value is :1500000
value is :2000000
2-1000000 entried added/retrieved in 90 ms
value is :2500000
value is :3000000
3-1000000 entried added/retrieved in 92 ms
value is :3500000
value is :4000000
4-1000000 entried added/retrieved in 94 ms
value is :4500000
value is :5000000
5-1000000 entried added/retrieved in 98 ms
For class java.util.Collections$SynchronizedMap the average time is 97 ms
Test started for: class java.util.concurrent.ConcurrentHashMap
value is :500000
value is :1000000
1-1000000 entried added/retrieved in 40 ms
value is :1500000
value is :2000000
2-1000000 entried added/retrieved in 35 ms
value is :2500000
value is :3000000
3-1000000 entried added/retrieved in 32 ms
value is :3500000
value is :4000000
4-1000000 entried added/retrieved in 34 ms
value is :4500000
value is :5000000
5-1000000 entried added/retrieved in 33 ms
For class java.util.concurrent.ConcurrentHashMap the average time is 34 ms
================================== map securicty test end ==================================
带着疑问再次进行单独测试:
Test started for: class java.util.HashMap
value is :500000
1-1000000 entried added/retrieved in 51 ms
value is :1000000
value is :1500000
2-1000000 entried added/retrieved in 40 ms
value is :2000000
value is :2500000
3-1000000 entried added/retrieved in 31 ms
value is :3000000
value is :3500000
4-1000000 entried added/retrieved in 30 ms
value is :4000000
value is :4500000
5-1000000 entried added/retrieved in 30 ms
For class java.util.HashMap the average time is 36 ms
发现这次就没有能够达到线程安全。
所以线程安全的策略有三种,而且要和atomic结合操作才能实现。
效率测试如下:
HashMap的效率最高,如果不考虑线程的问题(单线程使用的时候),HashMap是首选。
其次是ConcurrentHashMap,Collections$SynchronizedMap,Hashtable 。
一般情况下ConcurrentHashMap效率能够达到Collections$SynchronizedMap或者Hashtable 的两倍,
================================== map Efficience test start ==================================
Test started for: class java.util.HashMap
2500K entried added/retrieved in 58 ms
2500K entried added/retrieved in 49 ms
2500K entried added/retrieved in 26 ms
2500K entried added/retrieved in 27 ms
2500K entried added/retrieved in 28 ms
For class java.util.HashMap the average time is 37 ms
Test started for: class java.util.Hashtable
2500K entried added/retrieved in 113 ms
2500K entried added/retrieved in 76 ms
2500K entried added/retrieved in 72 ms
2500K entried added/retrieved in 75 ms
2500K entried added/retrieved in 74 ms
For class java.util.Hashtable the average time is 82 ms
Test started for: class java.util.Collections$SynchronizedMap
2500K entried added/retrieved in 100 ms
2500K entried added/retrieved in 87 ms
2500K entried added/retrieved in 88 ms
2500K entried added/retrieved in 84 ms
2500K entried added/retrieved in 87 ms
For class java.util.Collections$SynchronizedMap the average time is 89 ms
Test started for: class java.util.concurrent.ConcurrentHashMap
2500K entried added/retrieved in 63 ms
2500K entried added/retrieved in 58 ms
2500K entried added/retrieved in 58 ms
2500K entried added/retrieved in 57 ms
2500K entried added/retrieved in 60 ms
For class java.util.concurrent.ConcurrentHashMap the average time is 59 ms
================================== map Efficience test end ==================================
参考: http://blog.csdn.net/cnhk1225/article/details/52413201
- 如何实现线程的HashMap以及性能对比
- 三、如何实现线程安全的HashMap
- 【数据结构】常见的排序方法的实现以及性能对比
- HashMap遍历性能对比
- HashMap循环遍历方式及其性能对比 主要介绍HashMap的四种循环遍历方式,各种方式的性能测试对比,根据HashMap的源码实现分析性能结果,总结结论。
- java基础—HashMap实现原理,如何保证HashMap的线程安全?
- Hashmap的原理以及实现
- hashmap的底层以及实现
- HashMap原理,以及HashMap的非线程安全
- java HashMap遍历的三种方式以及效率对比
- 【Java基础】HashMap的四种遍历以及耗时对比
- Erlang实现的矩阵相乘C=A*B单线程与并行多线程性能对比
- 如何线程安全的使用HashMap
- 如何线程安全的使用HashMap
- 如何让HashMap变成线程安全的?
- 如何线程安全的使用 HashMap
- 如何线程安全的使用HashMap
- 如何线程安全的使用 HashMap
- Python:str字符串常用操作
- MFC中Unicode下汉字转换ULR串
- [android]am start [<OPTION>]... <INTENT>
- Windows 版 Crontab [JAVA]
- 掌握闭包closure
- 如何实现线程的HashMap以及性能对比
- Mysql不能插入中文和中文显示乱码问题
- 数据库设计:表的设计命名的十个注意点 转载
- springmvc+redis框架搭建
- 读《最好的告别:关于衰老与死亡,你必须知道的常识》
- Spring Web MVC
- PostgreSQL学习篇9.11 几何类型
- Mac下安装mxnet的方法及容易遇到的问题
- WPF DataGrid数据刷新