---
title: "验证Hashmap不支持同步，ConcurrentHashMap支持"
author: "xmind"
date: "2014-08-15T14:14:58Z"
tags: []
categories: ["编程语言"]
slug: "hashmap-concurrenthashmap"
draft: false
lastmod: "2014-08-15T14:14:58Z"
id: 2104332
url: "https://xmind.iteye.com/blog/2104332"
---

1. 一直都不知道concurrenthashmap有什么实际的用处？先写个例子比较下hashmap和它。
2. 方法用2000个线程下同一个key值，同步的话，应该最后的map的size为1，不同步可以大于1.
   
    Java Code 
   
   1  
   2  
   3  
   4  
   5  
   6  
   7  
   8  
   9  
   10  
   11  
   12  
   13  
   14  
   15  
   16  
   17  
   18  
   19  
   20  
   21  
   22  
   23  
   24  
   25  
   26  
   27  
   28  
   29  
   30  
   31
   
    
   
   public class HashMapSyn {  
       public static void main(String\[] args) throws InterruptedException {  
           System.out.println(runMapPutAndSizeShouldBe1(new HashMap&lt;String, String&gt;(32)));  
           System.out.println(runMapPutAndSizeShouldBe1(new ConcurrentHashMap&lt;String, String&gt;(32)));  
       }  
         
       public static int runMapPutAndSizeShouldBe1(final Map&lt;String, String&gt; map) throws InterruptedException {  
           Thread t = new Thread(new Runnable() {  
               @Override  
               public void run() {  
                   for (int i = 0; i &lt; 2000; i++) {  
                       new Thread(new Runnable() {  
                           @Override  
                           public void run() {  
                               map.put("1", "1");  
                           }  
                       }, "Thread inside " + i).start();  
                   }  
               }  
           }, "Thread outside");  
           t.start();  
           t.join();  
           return map.size();  
       }
   
       @Test  
       public void testHashMapNotSynchronize() throws InterruptedException {  
           Assert.assertEquals(1, runMapPutAndSizeShouldBe1(new ConcurrentHashMap&lt;String, String&gt;(32)));  
           Assert.assertNotSame(1, runMapPutAndSizeShouldBe1(new HashMap&lt;String, String&gt;(32)));  
       }  
   }
3. 当然，hashmap的最后可能测不出来，可以将hashmap拷到本来加Thread.sleep。
   
    Java Code 
   
   1  
   2  
   3  
   4  
   5  
   6  
   7  
   8  
   9  
   10  
   11  
   12  
   13  
   14  
   15  
   16  
   17  
   18  
   19  
   20  
   21  
   22
   
    
   
       public V put(K key, V value) {  
           if (key == null)  
               return putForNullKey(value);  
           int hash = hash(key.hashCode());  
           int i = indexFor(hash, table.length);  
           for (Entry&lt;K,V&gt; e = table\[i]; e != null; e = e.next) {  
               Object k;  
               if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {  
                   V oldValue = e.value;  
                   e.value = value;  
                   e.recordAccess(this);  
                   return oldValue;  
               }  
           }  
           modCount++;  
             
           /\** add sleep for test\*/  
           try { Thread.sleep(1); } catch (InterruptedException e1) {}  
             
           addEntry(hash, key, value, i);  
           return null;  
       }