ConcurrentHashMap vs Synchronized HashMap

后端 未结 12 2201
北海茫月
北海茫月 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 09:55

    Methods on SynchronizedMap hold the lock on the object, whereas in ConcurrentHashMap there's a concept of "lock striping" where locks are held on buckets of the contents instead. Thus improved scalability and performance.

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

    We can achieve thread safety by using both ConcurrentHashMap and synchronisedHashmap. But there is a lot of difference if you look at their architecture.

    1. synchronisedHashmap

    It will maintain the lock at the object level. So if you want to perform any operation like put/get then you have to acquire the lock first. At the same time, other threads are not allowed to perform any operation. So at a time, only one thread can operate on this. So the waiting time will increase here. We can say that performance is relatively low when you are comparing with ConcurrentHashMap.

    1. ConcurrentHashMap

    It will maintain the lock at the segment level. It has 16 segments and maintains the concurrency level as 16 by default. So at a time, 16 threads can be able to operate on ConcurrentHashMap. Moreover, read operation doesn't require a lock. So any number of threads can perform a get operation on it.

    If thread1 wants to perform put operation in segment 2 and thread2 wants to perform put operation on segment 4 then it is allowed here. Means, 16 threads can perform update(put/delete) operation on ConcurrentHashMap at a time.

    So that the waiting time will be less here. Hence the performance is relatively better than synchronisedHashmap.

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

    ConcurrentHashMap :

    1)Both maps are thread-safe implementations of the Map interface.

    2)ConcurrentHashMap is implemented for higher throughput in cases where high concurrency is expected.

    3) There is no locking in object level.

    Synchronized Hash Map:

    1) Each method is synchronized using an object level lock.

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

    The short answer:

    Both maps are thread-safe implementations of the Map interface. ConcurrentHashMap is implemented for higher throughput in cases where high concurrency is expected.

    Brian Goetz's article on the idea behind ConcurrentHashMap is a very good read. Highly recommended.

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

    SynchronizedMap and ConcurrentHashMap are both thread safe class and can be used in multithreaded application, the main difference between them is regarding how they achieve thread safety.

    SynchronizedMap acquires lock on the entire Map instance , while ConcurrentHashMap divides the Map instance into multiple segments and locking is done on those.

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

    ConcurrentHashMap allows concurrent access to data. Whole map is divided into segments.

    Read operation ie. get(Object key) is not synchronized even at segment level.

    But write operations ie. remove(Object key), get(Object key) acquire lock at segment level. Only part of whole map is locked, other threads still can read values from various segments except locked one.

    SynchronizedMap on the other hand, acquire lock at object level. All threads should wait for current thread irrespective of operation(Read/Write).

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