Sunday, April 24, 2011

Difference between ConcurrentHashMap, Hashtable and Synchronized Map in Java

ConcurrentHashMap vs Hashtable vs Synchronized Map
Though all three collection classes are thread-safe and can be used in multi-threaded, concurrent Java application, there is significant difference between them, which arise from the fact that how they achieve their thread-safety. Hashtable is a legacy class from JDK 1.1 itself, which uses synchronized methods to achieve thread-safety. All methods of Hashtable are synchronized which makes them quite slow due to contention if number of thread increases. Synchronized Map is also not very different than Hashtable and provides similar performance in concurrent Java programs. Only difference between Hashtable and Synchronized Map is that later is not a legacy and you can wrap any Map to create it's synchronized version by using Collections.synchronizedMap() method. On the other hand, ConcurrentHashMap is especially 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 internal implementation of ConcurrentHashMap class. Unlike Hashtable and Synchronized Map, it never locks whole Map, instead it divides the map in segments and locking is done on those. Though it perform better if number of reader threads is greater than number of writer threads. 

To be frank, 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 of Java application by using ArrayList where legacy codes were unnecessarily using Vector etc. Prior Java 5, One of the major drawback of Java Collection framework was lack of scalability. In multi-threaded Java application synchronized collection classes like Hashtable and Vector quickly becomes bottleneck; to address scalability 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, HashMap and synchronized Map and see difference between ConcurrentHashMap and Hashtable and synchronized Map in Java. We have already discussed some key difference between HashMap and Hashtable in Java in this blog and those will also help you to answer this question during interviews.

Why 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

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. This will be more clear by looking at this diagram which explains internal working of ConcurrentHashMap in Java.

Difference between ConcurrentHashMap, Hashtable and Synchronized Map in Java

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 synchronized Map. Here are some of common differences between ConcurrentHashMap and synchronized map in Java

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


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...


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...


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...


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.

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

Raj said...

Hi I got below question in interview, can some one help on this?

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...


Both(HashTable & Collections.SynschronizedMap()) are synchronized version of collection. Both have synchronized methods inside class. Both are blocking in nature i.e. multiple threads will need to wait for getting the lock on instance before putting/getting anything out of it.

So what is the difference. Well, NO major difference for above said reasons. Performance is also same for both collections.

Only thing which separates them is the fact HashTable is legacy class promoted into collection framework. It got its own extra features like enumerators.

Atmprakash Sharma said...

i have question ???
whose performance will be better ???

Javin Paul said...

@Atmprakash, it depends upon usage scenario. If you are running with just one two thread then not much difference, but if you are running with say 20 threads, ConcurrentHashMap will perform much better than Hashtable and Synchronized Map, because CHM uses lock stripping and multiple thread can read from Map simultaneously. See my post on how ConcurrentHashMap works internally for more details.

Anonymous said...

I was asked this question,

HashMap allows one null key and multiple null value.

So if I do:

Map m = new HashMap(null,"abc");

then how will I be able to retrieve this value "abc"?

Also in future if I do:

HashTable m1 = (HashTable)m;

since HashTable does not all null keys, what will happen in this case?

Gopal said...

Hi ,
You can't do like this HashTable m1 = (HashTable)m; .

If you want to put map into Hashtable you need call like this m1.putAll(m);
But If you do this you will get nullpointer exception like below. So Java taken care to handel null while converting Hashtable from map.

Exception in thread "main" java.lang.NullPointerException
at HashMapStructure.main(

Post a Comment