Sunday, April 24, 2011

Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java

Collections classes are heart of java API though I feel using them judiciously is an art. Its my personal experience where I have improved performance by using ArrayList where legacy codes are unnecessarily used Vector etc.
JDK 1.5 introduces some good concurrent collections which is highly efficient for high volume, low latency system electronic trading systems In general those are backbone for Concurrent fast access of stored data. In this tutorial we will look on ConcurrentHashMap, Hashtable and HashMap and Collections.synchronizedMap and see difference between ConcurrentHashMap and Hashtable and synchronizedMap.


This Java Collection tutorial is in continuation of my article How HashMap works in Java  and difference between HashMap and Hashtable in Java  if you haven’t read already you may find some useful information based on my experience in Java Collections.

Why we need ConcurrentHashMap and CopyOnWriteArrayList

The synchronized collections classes, Hashtable and Vector, and the synchronized wrapper classes, Collections.synchronizedMap and 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  for example 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 ConcurrentModificationException.

ConcurrentHashMap and CopyOnWriteArrayList implementations 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 HashMap or ArrayList, but are designed to optimize specific common situations. Many concurrent applications will benefit from their use. 

Difference between ConcurrentHashMap and Hashtable

difference between concurrentHashMap, hashtable and Synchronized Map
So what is the difference between Hashtable and ConcurrentHashMap , both can be used in multithreaded environment but once the size of Hashtable becomes considerable large performance degrade because for iteration it has to be locked for longer duration.

Since ConcurrentHashMap introduced concept of segmentation , how large it becomes only certain part of it get locked to provide thread safety so many other readers can still access map without waiting for iteration to complete.

In Summary ConcurrentHashMap only locked certain portion of Map while Hashtable lock full map while doing iteration. 

Difference between ConcurrentHashMap and Collections.synchronizedMap

ConcurrentHashMap is designed for concurrency and improve performance while HashMap which is non synchronized by nature can be synchronized by applying a wrapper using Collections.synchronizedMap. Here are some of common differences between ConcurrentHashMap and Collections.synchronizedMap

ConcurrentHashMap do not allow null keys or null values while HashMap allows null keys.

23 comments :

michee said...

this article is not very clear.
Collections.synchronizedMap locks the entire map or not ?

Javin @ java enum examples said...

Hi Michee, thanks for your comment. yes Collections.SynchronizedMap lock the entire Map while Concurrent Map only locks portion of Map.

Araminos said...

Am I able to write in ordinal HashMap from different threads and after all threads complete read results from one thread?

Javin @ String split Java example said...

Hi Araminos, As I have explained this in my post How HashMap works in Java . Write operation on HashMap can trigger resizing of HashMap and if re-size is done by multiple thread at same time that could lead to race condition in HashMap.

SreeMani said...

ConcurrentHashMap is more scalable than hashtable or SynchronizedMap. Also ConcurrentHashMap only locks portion of Map while SynchronizedMap and hashtable locks whole map which makes ConcurrentHashMap faster than hashtable or SynchronizedMap.

Anonymous said...

Good article.

Javin @ iterate hashmpa java said...

Thanks Anonymous , Glad you like this article on difference between ConcurrentHashMap and hashtable.

Ajith said...

Thanks javin . Great blog series. Here in ConcurrentHashMap What part gets locked on writes ? Is there some algorithm? Will be great if you could add that info . Also it would be great if you could discuss google java interview questions in a blog

Deepthi said...

Hi,

In an interview i was asked this question,
i have the option of using only array list.
How can i ensure thread safety using array list.

Anonymous said...

Deepthi,

By using CopyOnWriteArrayList. Collections.synchronizedList will be better, but as explained in this article, CopyOnWriteArrayList will be your best bet.

- Sankha

Anonymous said...

hi there! HashMap allows only one null key and many null values.(last stmt)
nice site! congrats

Javin @ hashtable example java said...

@Anonymous, Thanks. You are right, HashMap only allows one null key and many null values. As key can only be one. Once again thanks for making it more clear.

Arora On Top said...

Here one thing not clear, what is the difference between HashTable and collections.synchronizedMap(new HashMap()).I mean when we already have Hashtable in place to be used in concurrent environment, is not it collections.synchronizedMap(new HashMap())is the redundant feature?

Anonymous said...

Here one thing not clear, what is the difference between HashTable and collections.synchronizedMap(new HashMap()).I mean when we already have Hashtable in place to be used in concurrent environment, is not it collections.synchronizedMap(new HashMap())is the redundant feature?

Same Question? Is it just the scalibility issue of hashtable?

Teong Chian Teh said...

Hi,

I have a question which i dont understand. Does HashTable only returns Enumerator? and Enumerator is read only right? then how come it needs to lock the entire map?

Javin @ Sort Hashtable on values in Java said...

@Teong, Hashtable does return Entry Set as well, which can also be used to iterate over entries. By the way Hashtable provides keys() and elements() method which return Enumeration for sequential access of elements, these methods are synchronized and that's why locked current instance of Hashtable.

Saurabh Patil said...

When you say only a part of the map gets locked while using ConcurrentHashMap, which part gets locked? How is it decided that which part gets locked?

udit said...

@Saurabh, ConcurrentHashMap use Segments Policy to add the objects in the Map.
If I am not wrong, while putting Object in Map,It use that segement to lock, for which the Object key returns matching hash value.And this segment is locked using lock() method of ReentrantLock class. so the whole concept is , hashing value decides for any Key to find correct segment and only that segment will be locked.
Thanks

Srikanth said...

"... HashMap allows null keys."

HashMap allows atmost one null key, and there may be many null values.

Anonymous said...

I just went through few of your articles and they are really explaining things in a fine manner. Keep up the good work!!

kodali srikanth said...

Can someone post different questions on concurret collections concepts.

Anonymous said...

It is worth mentioning that in Collections.synchronizedMap we have an option to pass the mutex on which we want the lock while on hashtable we don't have. Consider the scenario if you want to have two maps which are synchronized for each other as well. like if 1 thread putting in one map then another thread should wait to put in 2nd map. Here we can make use of collection.synchronizedmap by passing same mutex to both the maps.

Anonymous said...

in concurrenthashmap and synchronizedmp these two which one is better and why

Post a Comment