验证Hashmap不支持同步,ConcurrentHashMap支持
  sAAkk3Vxfaa8 2023年11月02日 44 0


  1. 一直都不知道concurrenthashmap有什么实际的用处?先写个例子比较下hashmap和它。
  2. 方法用2000个线程下同一个key值,同步的话,应该最后的map的size为1,不同步可以大于1.  Java Code 
public           class HashMapSyn {          
              public           static           void main(          String[] args)           throws InterruptedException {          
        System.out.println(runMapPutAndSizeShouldBe1(          new HashMap<          String,           String>(          32)));          
        System.out.println(runMapPutAndSizeShouldBe1(          new ConcurrentHashMap<          String,           String>(          32)));          
    }          
              
              public           static           int runMapPutAndSizeShouldBe1(          final Map<          String,           String> map)           throws InterruptedException {          
        Thread t =           new Thread(          new Runnable() {          
            @Override          
                      public           void run() {          
                          for (          int i =           0; i <           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<          String,           String>(          32)));          
        Assert.assertNotSame(          1, runMapPutAndSizeShouldBe1(          new HashMap<          String,           String>(          32)));          
    }          
}

  1. 当然,hashmap的最后可能测不出来,可以将hashmap拷到本来加Thread.sleep。  Java Code 
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<K,V> 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;          
    }
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: Eclipse 3.4新特性 - Plug-in spy 下一篇: c class
  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   55   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   110   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
sAAkk3Vxfaa8