HashMap和ConcurrentHashMap的并发性能测试
来源:互联网 发布:php.ini在哪个文件夹 编辑:程序博客网 时间:2024/06/05 07:31
先看看代码吧,模拟1000个并发,每个测试1000次操作,循环测试10轮。分别测试Put和Get操作
Put time HashMapSync=3966ms.
Put time ConcurrentHashMap=1892ms.
Put time Hashtable=3892ms.
Get time HashMapSync=3812ms.
Get time ConcurrentHashMap=1828ms.
Get time Hashtable=3985ms.
结论:
ConcurrentHashMap的性能比同步的HashMap快一倍左右
同步的HashMap和Hashtable的性能相当
import java.util.Collections; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class T { static final int threads = 1000; static final int NUMBER = 1000; public static void main(String[] args) throws Exception { Map<String, Integer> hashmapSync = Collections .synchronizedMap(new HashMap<String, Integer>());//同步的hashmap Map<String, Integer> concurrentHashMap = new ConcurrentHashMap<String, Integer>(); Map<String, Integer> hashtable = new Hashtable<String, Integer>(); long totalA = 0; long totalB = 0; long totalC = 0; for (int i = 0; i <= 10; i++) { totalA += testPut(hashmapSync); totalB += testPut(concurrentHashMap); totalC += testPut(hashtable); } System.out.println("Put time HashMapSync=" + totalA + "ms."); System.out.println("Put time ConcurrentHashMap=" + totalB + "ms."); System.out.println("Put time Hashtable=" + totalC + "ms."); totalA = 0; totalB = 0; totalC = 0; for (int i = 0; i <= 10; i++) { totalA += testGet(hashmapSync); totalB += testGet(concurrentHashMap); totalC += testGet(hashtable); } System.out.println("Get time HashMapSync=" + totalA + "ms."); System.out.println("Get time ConcurrentHashMap=" + totalB + "ms."); System.out.println("Get time Hashtable=" + totalC + "ms."); } public static long testPut(Map<String, Integer> map) throws Exception { long start = System.currentTimeMillis(); for (int i = 0; i < threads; i++) { new MapPutThread(map).start(); } while (MapPutThread.counter > 0) { Thread.sleep(1); } return System.currentTimeMillis() - start; } public static long testGet(Map<String, Integer> map) throws Exception { long start = System.currentTimeMillis(); for (int i = 0; i < threads; i++) { new MapPutThread(map).start(); } while (MapPutThread.counter > 0) { Thread.sleep(1); } return System.currentTimeMillis() - start; } } class MapPutThread extends Thread { static int counter = 0; static Object lock = new Object(); private Map<String, Integer> map; private String key = this.getId() + ""; MapPutThread(Map<String, Integer> map) { synchronized (lock) { counter++; } this.map = map; } public void run() { for (int i = 1; i <= T.NUMBER; i++) { map.put(key, i); } synchronized (lock) { counter--; } } } class MapGetThread extends Thread { static int counter = 0; static Object lock = new Object(); private Map<String, Integer> map; private String key = this.getId() + ""; MapGetThread(Map<String, Integer> map) { synchronized (lock) { counter++; } this.map = map; } public void run() { for (int i = 1; i <= T.NUMBER; i++) { map.get(key); } synchronized (lock) { counter--; } } }运行结果:
Put time HashMapSync=3966ms.
Put time ConcurrentHashMap=1892ms.
Put time Hashtable=3892ms.
Get time HashMapSync=3812ms.
Get time ConcurrentHashMap=1828ms.
Get time Hashtable=3985ms.
结论:
ConcurrentHashMap的性能比同步的HashMap快一倍左右
同步的HashMap和Hashtable的性能相当
0 0
- HashMap和ConcurrentHashMap的并发性能测试
- HashMap和ConcurrentHashMap的并发性能测试
- HashMap和ConcurrentHashMap的并发性能测试
- 测试HashMap和ConcurrentHashMap的并发性能差别
- Java中的HashMap和ConcurrentHashMap的并发性能测试
- ConcurrentHashMap(并发hashmap)的使用
- HashMap和SynchronizedMap和ConcurrentHashMap性能比较
- ConcurrentHashMap和HashMap的区别
- ConcurrentHashMap的原理和HashMap
- HashMap和ConcurrentHashMap的对比
- HashMap和ConcurrentHashMap的对比
- ConcurrentHashMap和HashMap的区别
- HashMap和ConcurrentHashMap的区别
- HashMap和ConcurrentHashMap的浅析
- HashTable和HashMap和ConcurrentHashMap的区别
- ConcurrentHashMap并发性测试
- ConcurrentHashMap 高并发、高吞吐量的线程安全HashMap实现
- HashMap HashTable和ConcurrentHashMap的区别
- linux网络编程之IO复用
- 规则化和模型选择(Regularization and model selection)
- 什么原因成就了一位优秀的程序员
- 还没写过文章 随便写点吧
- C# 特性详解
- HashMap和ConcurrentHashMap的并发性能测试
- JAVA学习之多态(一)
- 分布式版本控制工具——Git
- myeclipse中的转为Web项目
- 检测应用程序是否已经安装
- MapReduce中的自定义多目录/文件名输出<转>
- 业务逻辑实现方式选择
- 混合高斯模型(Mixtures of Gaussians)和EM算法
- Android AIDL