What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?

前端 未结 19 891
名媛妹妹
名媛妹妹 2020-11-22 11:45

I have a Map which is to be modified by several threads concurrently.

There seem to be three different synchronized Map implementations in the Java API:

    <
相关标签:
19条回答
  • 2020-11-22 12:04

    Collections.synchronizedMap() method synchronizes all the methods of the HashMap and effectively reduces it to a data structure where one thread can enter at a time because it locks every method on a common lock.

    In ConcurrentHashMap synchronization is done a little differently. Rather than locking every method on a common lock, ConcurrentHashMap uses separate lock for separate buckets thus locking only a portion of the Map. By default there are 16 buckets and also separate locks for separate buckets. So the default concurrency level is 16. That means theoretically any given time 16 threads can access ConcurrentHashMap if they all are going to separate buckets.

    0 讨论(0)
  • 2020-11-22 12:05

    ConcurrentHashMap

    • You should use ConcurrentHashMap when you need very high concurrency in your project.
    • It is thread safe without synchronizing the whole map.
    • Reads can happen very fast while write is done with a lock.
    • There is no locking at the object level.
    • The locking is at a much finer granularity at a hashmap bucket level.
    • ConcurrentHashMap doesn’t throw a ConcurrentModificationException if one thread tries to modify it while another is iterating over it.
    • ConcurrentHashMap uses multitude of locks.

    SynchronizedHashMap

    • Synchronization at Object level.
    • Every read/write operation needs to acquire lock.
    • Locking the entire collection is a performance overhead.
    • This essentially gives access to only one thread to the entire map & blocks all the other threads.
    • It may cause contention.
    • SynchronizedHashMap returns Iterator, which fails-fast on concurrent modification.

    source

    0 讨论(0)
  • 2020-11-22 12:05

    In general, if you want to use the ConcurrentHashMap make sure you are ready to miss 'updates'
    (i.e. printing contents of the HashMap does not ensure it will print the up-to-date Map) and use APIs like CyclicBarrier to ensure consistency across your program's lifecycle.

    0 讨论(0)
  • 2020-11-22 12:07

    The "scalability issues" for Hashtable are present in exactly the same way in Collections.synchronizedMap(Map) - they use very simple synchronization, which means that only one thread can access the map at the same time.

    This is not much of an issue when you have simple inserts and lookups (unless you do it extremely intensively), but becomes a big problem when you need to iterate over the entire Map, which can take a long time for a large Map - while one thread does that, all others have to wait if they want to insert or lookup anything.

    The ConcurrentHashMap uses very sophisticated techniques to reduce the need for synchronization and allow parallel read access by multiple threads without synchronization and, more importantly, provides an Iterator that requires no synchronization and even allows the Map to be modified during interation (though it makes no guarantees whether or not elements that were inserted during iteration will be returned).

    0 讨论(0)
  • 2020-11-22 12:07

    Here are few :

    1) ConcurrentHashMap locks only portion of Map but SynchronizedMap locks whole MAp.
    2) ConcurrentHashMap has better performance over SynchronizedMap and more scalable.
    3) In case of multiple reader and Single writer ConcurrentHashMap is best choice.

    This text is from Difference between ConcurrentHashMap and hashtable in Java

    0 讨论(0)
  • 2020-11-22 12:08

    For your needs, use ConcurrentHashMap. It allows concurrent modification of the Map from several threads without the need to block them. Collections.synchronizedMap(map) creates a blocking Map which will degrade performance, albeit ensure consistency (if used properly).

    Use the second option if you need to ensure data consistency, and each thread needs to have an up-to-date view of the map. Use the first if performance is critical, and each thread only inserts data to the map, with reads happening less frequently.

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