ITeye博客

HashMap和ConcurrentHashMap的并发性能测试

jguangyou · 2011-06-13 10:29 · java基础 · 编程语言 · Markdown

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

  1. import java.util.Collections;
  2. import java.util.HashMap;
  3. import java.util.Hashtable;
  4. import java.util.Map;
  5. import java.util.concurrent.ConcurrentHashMap;
  6. public class T {
  7. static final int threads = 1000;
  8. static final int NUMBER = 1000;
  9. public static void main(String[] args) throws Exception {
  10. Map<String, Integer> hashmapSync = Collections
  11. .synchronizedMap(new HashMap<String, Integer>());//同步的hashmap
  12. Map<String, Integer> concurrentHashMap = new ConcurrentHashMap<String, Integer>();
  13. Map<String, Integer> hashtable = new Hashtable<String, Integer>();
  14. long totalA = 0;
  15. long totalB = 0;
  16. long totalC = 0;
  17. for (int i = 0; i <= 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 <= 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<String, Integer> map) throws Exception {
  38. long start = System.currentTimeMillis();
  39. for (int i = 0; i < threads; i++) {
  40. new MapPutThread(map).start();
  41. }
  42. while (MapPutThread.counter > 0) {
  43. Thread.sleep(1);
  44. }
  45. return System.currentTimeMillis() - start;
  46. }
  47. public static long testGet(Map<String, Integer> map) throws Exception {
  48. long start = System.currentTimeMillis();
  49. for (int i = 0; i < threads; i++) {
  50. new MapPutThread(map).start();
  51. }
  52. while (MapPutThread.counter > 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<String, Integer> map;
  62. private String key = this.getId() + "";
  63. MapPutThread(Map<String, Integer> map) {
  64. synchronized (lock) {
  65. counter++;
  66. }
  67. this.map = map;
  68. }
  69. public void run() {
  70. for (int i = 1; i <= 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<String, Integer> map;
  82. private String key = this.getId() + "";
  83. MapGetThread(Map<String, Integer> map) {
  84. synchronized (lock) {
  85. counter++;
  86. }
  87. this.map = map;
  88. }
  89. public void run() {
  90. for (int i = 1; i <= 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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。