ConcurrentHashMap vs Synchronized HashMap

后端 未结 12 2198
北海茫月
北海茫月 2020-11-27 09:50

What is the difference between using the wrapper class, SynchronizedMap, on a HashMap and ConcurrentHashMap?

Is it just bein

相关标签:
12条回答
  • 2020-11-27 10:07

    Synchronized HashMap

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

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

    ConcurrentHashMap was introduced in JDK 5.

    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.

    2. 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.

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

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

    0 讨论(0)
  • 2020-11-27 10:08

    As per java doc's

    Hashtable and Collections.synchronizedMap(new HashMap()) are synchronized. But ConcurrentHashMap is "concurrent".

    A concurrent collection is thread-safe, but not governed by a single exclusion lock.

    In the particular case of ConcurrentHashMap, it safely permits any number of concurrent reads as well as a tunable number of concurrent writes. "Synchronized" classes can be useful when you need to prevent all access to a collection via a single lock, at the expense of poorer scalability.

    In other cases in which multiple threads are expected to access a common collection, "concurrent" versions are normally preferable. And unsynchronized collections are preferable when either collections are unshared, or are accessible only when holding other locks.

    0 讨论(0)
  • 2020-11-27 10:11

    A simple performance test for ConcurrentHashMap vs Synchronized HashMap . The test flow is calling put in one thread and calling get in three threads on Map concurrently. As @trshiv said, ConcurrentHashMap has higher throughput and speed for whose reading operation without lock. The result is when operation times is over 10^7, ConcurrentHashMap is 2x faster than Synchronized HashMap.

    0 讨论(0)
  • 2020-11-27 10:12

    ConcurrentHashMap is thread safe without synchronizing the whole map. Reads can happen very fast while write is done with a lock.

    0 讨论(0)
  • 2020-11-27 10:16

    Both are synchronized version of HashMap, with difference in their core functionality and their internal structure.

    ConcurrentHashMap consist of internal segments which can be viewed as independent HashMaps Conceptually. All such segments can be locked by separate threads in high concurrent executions. So, multiple threads can get/put key-value pairs from ConcurrentHashMap without blocking/waiting for each other. This is implemented for higher throughput.

    whereas

    Collections.synchronizedMap(), we get a synchronized version of HashMap and it is accessed in blocking manner. This means if multiple threads try to access synchronizedMap at same time, they will be allowed to get/put key-value pairs one at a time in synchronized manner.

    0 讨论(0)
  • 2020-11-27 10:19

    ConcurrentHashMap uses finer-grained locking mechanism known as lock stripping to allow greater degree of shared access. Due to this it provides better concurrency and scalability.

    Also iterators returned for ConcurrentHashMap are weakly consistent instead of fail fast technique used by Synchronized HashMap.

    0 讨论(0)
提交回复
热议问题