ConcurrentHashMap与同步HashMap [英] ConcurrentHashMap vs Synchronized HashMap

查看:159
本文介绍了ConcurrentHashMap与同步HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HashMap SynchronizedMap 之间有什么区别>的ConcurrentHashMap ?
是否能够在迭代它时修改 HashMap ConcurrentHashMap )?

What is the difference between using the wrapper class, SynchronizedMap, on a HashMap and ConcurrentHashMap? Is it just being able to modify the HashMap while iterating it (ConcurrentHashMap)?

推荐答案

同步 HashMap


  1. 使用对象级锁定同步每个方法。因此,synchMap上的get和put方法获得了锁定。

  1. Each method is synchronized using an object level lock. So the get and put methods on synchMap acquire a lock.

锁定整个集合是一种性能开销。当一个线程持有锁时,没有其他线程可以使用该集合。

Locking the entire collection is a performance overhead. While one thread holds on to the lock, no other thread can use the collection.

ConcurrentHashMap 是在JDK 5中引入的。

ConcurrentHashMap was introduced in JDK 5.


  1. 对象级别没有锁定,锁定是一个更精细的粒度。对于 ConcurrentHashMap ,锁可能处于散列图桶级别。

  1. There is no locking at the object level,The locking is at a much finer granularity. For a ConcurrentHashMap, the locks may be at a hashmap bucket level.

较低级别的影响锁定是指您可以拥有并发读取器和编写器,这对于同步集合是不可能的。这会带来更大的可伸缩性。

The effect of lower level locking is that you can have concurrent readers and writers which is not possible for synchronized collections. This leads to much more scalability.

ConcurrentHashMap 不会抛出 ConcurrentModificationException 如果一个线程试图修改它而另一个线程迭代它。

ConcurrentHashMap does not throw a ConcurrentModificationException if one thread tries to modify it while another is iterating over it.

本文 Java 7:HashMap vs ConcurrentHashMap 是一个非常好的阅读。强烈推荐。

This article Java 7: HashMap vs ConcurrentHashMap is a very good read. Highly recommended.

这篇关于ConcurrentHashMap与同步HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆