HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

corejava-collection: What is Concurrent HashMap?

Please explain Concurrent HashMap and where it is used?

corejava x 353
collection x 52
Posted On : 2014-05-02 12:52:32.0
profile Rishi Kumar - Rishi Kumar


ConcurrentHashMap was introduced as an alternative of Hashtable in Java5 as part of Java concurrency package. If you need a Map implementation, which can be used safely in a concurrent and multi-threaded Java program, then, you only have Hashtable or synchronized Map because HashMap is not thread-safe. ConcurrentHashMap is better because, it is not only can be safely used in concurrent multi-threaded environment but also provides better performance over Hashtable and synchronizedMap because it only locks a portion of Map, instead of whole Map, which is the issue with Hashtable and synchronized Map. It allows concurred read operations and maintains integrity by synchronizing write operations.

ConcurrentHashMap implementation in Java:
As it is alternative of Hashtable and synchronized Map, so all the features supported by Hashtable with additional feature called "concurrency level", which allows it to partition Map. ConcurrentHashMap allows multiple useers to read concurrently without any blocking. This is achieved by partitioning Map based on the concurrency level and locking only a portion of Map during updates. Default concurrency level is 16, and accordingly Map is divided into 16 part and each part is operated with different lock. This means, 16 thread simultaneously can operate on Map, until they are operating on different part of Map. It makes ConcurrentHashMap high performance despite keeping thread-safety intact. Since update operations like put(), putAll(), remove() or clear() is not synchronized, concurrent retrieval may not reflect most recent change on Map.

In case of putAll() or clear(), which operates on whole Map, concurrent read may reflect insertion and removal of only some entries. Another important point to be remembered is iteration over ConcurrentHashMap, Iterator returned by keySet of ConcurrentHashMap are weekly consistent and they only reflect state of ConcurrentHashMap and certain point and may not reflect any recent change. Iterator of ConcurrentHashMap´s keySet area also fail-safe and doesn?t throw ConcurrentModificationExceptoin.

Default concurrency level is 16 and can be changed, by providing a number which make sense and work for you while creating ConcurrentHashMap. Since concurrency level is used for internal sizing and indicate number of concurrent update without contention, so, if you just have few writers or thread to update Map keeping it low is much better. ConcurrentHashMap also uses ReentrantLock to internally lock its segments.

When to use ConcurrentHashMap in Java:
ConcurrentHashMap is best suited when you have multiple readers and few writers. If writers exceeds reader, or writer is equal to reader, than performance of ConcurrentHashMap effectively reduces to synchronized map or Hashtable. Performance of CHM drops, because you got to lock all portion of Map, and effectively each reader will wait for another writer, operating on that portion of Map. ConcurrentHashMap is a good choice for caches, which can be initialized during application start up and later accessed my many request processing threads. As javadoc states, CHM is also a good replacement of Hashtable and should be used whenever possible, keeping in mind, that CHM provides slightly weeker form of synchronization than Hashtable.

Posted On : 2014-05-02 14:45:58
Satisfied : 1 Yes  0 No
profile Garima Gupta - Garima Gupta
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -