Map的一些实现类有及其特性

线程安全 特性

Hashtable

Key不能为null

HashMap

读写效率最高,但在Java6多线程环境下使用不当可能陷入死循环,进而导致CPU使用率过高(原理可参见:http://coolshell.cn/articles/9606.html)

Collections.synchronizedMap

Collections.SynchronizedMap在Map所有方法基础上加锁,效率与HashTable相当

ConcurrentHashMap

采用分段锁,get一般不加锁,put分段锁,key/value不能为null,效率仅次于HashMap

以下代码测试各类的读写效率:

package com.concurrent.test;

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; /**
* 测试map
*/
public class ThreadMapTest { public static void main(String[] args) throws InterruptedException {
Map<Integer, Integer> hashtable = new Hashtable<>();
Map<Integer, Integer> hashmap = new HashMap<>();
Map<Integer, Integer> synchronizedHashMap = Collections.synchronizedMap(new HashMap<Integer, Integer>());
Map<Integer, Integer> concurrentHashMap = new ConcurrentHashMap<>(); test(hashtable);
test(hashmap);
test(synchronizedHashMap);
test(concurrentHashMap);
} private static void test(Map<Integer, Integer> map) throws InterruptedException {
int testTimes = 5;
long totalTimeMillis = 0;
for (int k = 0; k < testTimes; k++) {
totalTimeMillis += costTimeMillis(map);
}
System.out.println("Test " + map.getClass() + " average time " + (totalTimeMillis / testTimes));
} private static long costTimeMillis(final Map<Integer, Integer> map) throws InterruptedException {
int count = 5;
ExecutorService executorService = Executors.newFixedThreadPool(count);
long startMillis = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
executorService.execute(new Runnable() { @Override
public void run() {
for (int j = 0; j < 50000; j++) {
map.put(0, 0);
map.get(0);
}
}
}); }
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
return System.currentTimeMillis() - startMillis;
} }

输出结果如下:

Test class java.util.Hashtable average time 104
Test class java.util.HashMap average time 31
Test class java.util.Collections$SynchronizedMap average time 79
Test class java.util.concurrent.ConcurrentHashMap average time 66

最新文章

  1. Parameter Passing / Request Parameters in JSF 2.0 (转)
  2. 不引用office动态库导出excel
  3. xcode6 使用MJRefresh
  4. kafka安装与使用
  5. 磁珠(FB)的原理
  6. 内核空间和用户空间的分界 PAGE_OFFSET
  7. BZOJ 3924: [Zjoi2015]幻想乡战略游戏(动态点分治)
  8. CF #228 div1 B. Fox and Minimal path
  9. ElasticSearch 插件jdbc import(1)-----定时执行
  10. ubuntu实时显示网速cpu占用和内存占用率
  11. MFC 中线程传递CString 是不安全的 转
  12. ios WKWebView 与 JS 交互实战技巧
  13. 深度理解 React Suspense(附源码解析)
  14. Centos防火墙设置与端口开放的方法
  15. JAVA项目中的常用的异常处理
  16. 笔记 Bioinformatics Algorithms Chapter7
  17. android 获取 imei号码
  18. 初识Identity并添加身份验证管理页面
  19. ZBar与ZXing使用后感觉
  20. 【Ubuntu】Windows硬盘安装Ubuntu14.04

热门文章

  1. 漫谈C语言结构体
  2. Area的使用
  3. 数据持久化之Android文件系统(一)
  4. [FW]CLONE_NEWUSER trickery: CVE-2013-1858
  5. 【WPF】一些拖拽实现方法的总结(Window,UserControl)
  6. Linux环境安装Nginx步骤
  7. ZOJ-3524 拓扑排序+完全背包(好题)
  8. Codeforces 1140F 线段树 分治 并查集
  9. [转载]MinGW安装过程
  10. STL_set