---
title: "HashMap和ConcurrentHashMap的并发性能测试"
author: "jguangyou"
date: "2011-06-13T10:29:09Z"
tags: []
categories: ["编程语言"]
slug: "hashmap-concurrenthashmap"
draft: false
lastmod: "2011-06-13T10:29:09Z"
id: 1083169
url: "https://deep-fish.iteye.com/blog/1083169"
---

先看看代码吧，模拟1000个并发，每个测试1000次操作，循环测试10轮。分别测试Put和Get操作 

01. import java.util.Collections;
02. import java.util.HashMap;
03. import java.util.Hashtable;
04. import java.util.Map;
05. import java.util.concurrent.ConcurrentHashMap;
06. public class T {
07. static final int threads = 1000;
08. static final int NUMBER = 1000;
09. public static void main(String\[] args) throws Exception {
10. Map&lt;String, Integer&gt; hashmapSync = Collections
11. .synchronizedMap(new HashMap&lt;String, Integer&gt;());//同步的hashmap
12. Map&lt;String, Integer&gt; concurrentHashMap = new ConcurrentHashMap&lt;String, Integer&gt;();
13. Map&lt;String, Integer&gt; hashtable = new Hashtable&lt;String, Integer&gt;();
14. long totalA = 0;
15. long totalB = 0;
16. long totalC = 0;
17. for (int i = 0; i &lt;= 10; i++) {
18. totalA += testPut(hashmapSync);
19. totalB += testPut(concurrentHashMap);
20. totalC += testPut(hashtable);
21. }
22. System.out.println("Put time HashMapSync=" + totalA + "ms.");
23. System.out.println("Put time ConcurrentHashMap=" + totalB + "ms.");
24. System.out.println("Put time Hashtable=" + totalC + "ms.");
25. totalA = 0;
26. totalB = 0;
27. totalC = 0;
28. for (int i = 0; i &lt;= 10; i++) {
29. totalA += testGet(hashmapSync);
30. totalB += testGet(concurrentHashMap);
31. totalC += testGet(hashtable);
32. }
33. System.out.println("Get time HashMapSync=" + totalA + "ms.");
34. System.out.println("Get time ConcurrentHashMap=" + totalB + "ms.");
35. System.out.println("Get time Hashtable=" + totalC + "ms.");
36. }
37. public static long testPut(Map&lt;String, Integer&gt; map) throws Exception {
38. long start = System.currentTimeMillis();
39. for (int i = 0; i &lt; threads; i++) {
40. new MapPutThread(map).start();
41. }
42. while (MapPutThread.counter &gt; 0) {
43. Thread.sleep(1);
44. }
45. return System.currentTimeMillis() - start;
46. }
47. public static long testGet(Map&lt;String, Integer&gt; map) throws Exception {
48. long start = System.currentTimeMillis();
49. for (int i = 0; i &lt; threads; i++) {
50. new MapPutThread(map).start();
51. }
52. while (MapPutThread.counter &gt; 0) {
53. Thread.sleep(1);
54. }
55. return System.currentTimeMillis() - start;
56. }
57. }
58. class MapPutThread extends Thread {
59. static int counter = 0;
60. static Object lock = new Object();
61. private Map&lt;String, Integer&gt; map;
62. private String key = this.getId() + "";
63. MapPutThread(Map&lt;String, Integer&gt; map) {
64. synchronized (lock) {
65. counter++;
66. }
67. this.map = map;
68. }
69. public void run() {
70. for (int i = 1; i &lt;= T.NUMBER; i++) {
71. map.put(key, i);
72. }
73. synchronized (lock) {
74. counter--;
75. }
76. }
77. }
78. class MapGetThread extends Thread {
79. static int counter = 0;
80. static Object lock = new Object();
81. private Map&lt;String, Integer&gt; map;
82. private String key = this.getId() + "";
83. MapGetThread(Map&lt;String, Integer&gt; map) {
84. synchronized (lock) {
85. counter++;
86. }
87. this.map = map;
88. }
89. public void run() {
90. for (int i = 1; i &lt;= T.NUMBER; i++) {
91. map.get(key);
92. }
93. synchronized (lock) {
94. counter--;
95. }
96. }
97. }

运行结果：  
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的性能相当 

转自：[http://blog.csdn.net/java2000\_net/archive/2008/11/25/3373181.aspx](http://blog.csdn.net/java2000_net/archive/2008/11/25/3373181.aspx)