HashMap和SynchronizedMap和ConcurrentHashMap性能比较
来源:互联网 发布:chrome浏览器 windows 编辑:程序博客网 时间:2024/05/23 16:51
怎么证明HashMap和SynchronizedMap和ConcurrentHashMap性能优劣
代码如下:
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;public class HashTableVsSynchronizedMapVsConcurrentHashMap {public final static int THREAD_POOL_SIZE = 5;public static Map<String, Integer> hashTableObject = null;public static Map<String, Integer> synchronizedMapObject = null;public static Map<String, Integer> concurrentHashMapObject = null;public static void main(String[] args) throws InterruptedException {// 测试HashTablehashTableObject = new Hashtable<String, Integer>();performTest(hashTableObject);// 测试synchronizedMapsynchronizedMapObject = Collections.synchronizedMap(new HashMap<String, Integer>());performTest(synchronizedMapObject); // 测试ConcurrentHashMap concurrentHashMapObject = new ConcurrentHashMap<String, Integer>(); performTest(concurrentHashMapObject); }private static void performTest(final Map<String, Integer> map) throws InterruptedException {System.err.println("开始测试 : " + map.getClass());long averageTime = 0;for (int i = 0; i < 5; i++) {long startTime = System.nanoTime();ExecutorService es = Executors.newFixedThreadPool(THREAD_POOL_SIZE);for (int j = 0; j < THREAD_POOL_SIZE; j++) {es.execute(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 500000; i++) {Integer randomNumber = (int) Math.ceil(Math.random() * 550000);map.put(String.valueOf(randomNumber), randomNumber);}}});}es.shutdown();es.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);long entTime = System.nanoTime();long totalTime = (entTime - startTime) / 1000000L;averageTime += totalTime;System.out.println("添加耗时" + totalTime + " ms");}System.out.println("操作 " + map.getClass() + " 平均时间为 " + averageTime / 5 + " ms\n");}}
0 0
- HashMap和SynchronizedMap和ConcurrentHashMap性能比较
- ConcurrentHashMap 和 Collections.synchronizedMap(map) 比较
- Hashtable 和 HashMap和concurrentHashMap和collections.synchronizedMap
- HashMap和ConcurrentHashMap比较
- HashMap和ConcurrentHashMap比较
- SynchronizedMap和ConcurrentHashMap分析
- HashMap,HashTable,synchronizedMap,ConcurrentHashMap,TreeMap,IdentityHashMap的比较分析
- Hashtable、synchronizedMap、ConcurrentHashMap 比较
- Hashtable、synchronizedMap、ConcurrentHashMap 比较
- Hashtable、synchronizedMap、ConcurrentHashMap 比较
- Hashtable、synchronizedMap、ConcurrentHashMap 比较
- Hashtable、synchronizedMap、ConcurrentHashMap 比较
- Hashtable、synchronizedMap、ConcurrentHashMap 比较
- Hashtable、synchronizedMap、ConcurrentHashMap 比较
- Hashtable、synchronizedMap、ConcurrentHashMap 比较
- Hashtable、synchronizedMap、ConcurrentHashMap 比较
- Hashtable、synchronizedMap、ConcurrentHashMap比较
- Hashtable、synchronizedMap、ConcurrentHashMap 比较
- linux下的连接管理工具xshell的使用
- JVM进阶(十)——JAVA 年老代收集器
- DOM文档对象模型
- SQL单行函数-日期函数、转换函数、通用函数
- Mybatis(一)、mybatis环境搭建以及实体类及其配置
- HashMap和SynchronizedMap和ConcurrentHashMap性能比较
- eclipse报HttpServletRequest cannot be resolved to a type错误
- hadoop伪分布式环境搭建
- office 2013 vol版180天后重新激活
- 关于"多态"形式的成员变量问题(关于this究竟代表的案例)
- 抽象类和接口
- 布局管理
- iOS开发 支持https请求以及https请求的抓包
- Eclipse导入maven没有maven选项