Performance of HashMap, HashTable, SynchronizedHashMap and ConcurrentHashMap


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.
I highly recommend to follow the below link. Clearly explained about the difference in the performance between HashMap, HashTable, SynchronizedHashMap and ConcurrentHashMap when they were used in multi threaded environments.
https://dzone.com/articles/java-7-hashmap-vs


Comments

Popular posts from this blog

Bash - Execute Pl/Sql script from Shell script

How to get client Ip Address using Java HttpServletRequest

How to install Portable JDK in Windows without Admin rights