There are number of question in mind when we think about
ConcurrentHashMap. All uses Map data structure to keep the data but one main difference is locking. Let us go one by one as follows.
HashMapis an implementation of Map data structure
- for single threaded environment
- Not thread-safe
- can keep one null key and multiple null value.
Hashtableis an implementation of Map data structure
- This is a legacy class in which all methods are synchronized on Hashtable instances using synchronized keyword.
- Thread-safe as it’s method are synchronized
- This is wrapper of Map, and can be initialize by using
- similar to
- The only difference between
Hashtableand Synchronized Map is that later is not a legacy and you can wrap any Map to create it’s synchronized version by using
ConcurrentHashMapimplements Map data structure and also provide thread safety like Hashtable.
- It works by dividing complete hashtable array in to segments or portions and allowing parallel access to those segments.
- The locking is at a much finer granularity at a hashmap bucket level.
ConcurrentHashMapwhen you need very high concurrency in your application.
- It is thread-safe without synchronizing the whole map.
- Reads can happen very fast while write is done with a lock on segment level or bucket level.
- There is no locking at the object level.
ConcurrentHashMapdoesn’t throw a
ConcurrentModificationExceptionif one thread tries to modify it while another is iterating over it.
ConcurrentHashMapdoes not allow NULL values, so key can not be null in
ConcurrentHashMapdoesn’t throw a
ConcurrentModificationExceptionif one thread tries to modify it, while another is iterating over it.
5. Difference between Hashtable, HashMap and concurrent HashMap[table “” not found /]
6. Putting It Together
CopyOnWriteArrayListimplementations provide much higher concurrency while preserving thread safety, with some minor compromises in their promises to callers. ConcurrentHashMap and CopyOnWriteArrayList are not necessarily useful everywhere you might use
ArrayList, but are designed to optimize specific common situations. Many concurrent applications will benefit from their use.
- The synchronized collections classes, Hashtable, and Vector, and the synchronized wrapper classes,
Collections.synchronizedList(), provide a basic conditionally thread-safe implementation of Map and List. However, several factors make them unsuitable for use in highly concurrent applications, e.g, their single collection-wide lock is an impediment to scalability and it often becomes necessary to lock a collection for a considerable time during iteration to prevent
ConcurrentHashMapis specially designed for concurrent use i.e. more than one thread. By default it simultaneously allows 16 threads to read and write from Map without any external
synchronization. It is also very scalable because of stripped locking technique used in the internal implementation of
Synchronized Map, it never locks whole Map, instead, it divides the map into segments and locking is done on those. Though it performs better if a number of reader threads are greater than the number of writer threads.
ConcurrentHashMapuses a database shards logic(Segment
 segments) , i.e. divide the data into shards(segments) than puts locks on each shard (segment) instead of putting single lock for whole data(Map)
Suggestions are welcome always to improve this post.