Tuesday, July 27, 2021

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 a 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 a number of thread increases. Synchronized Map is also not very different than Hashtable and provides similar performance in concurrent Java programs. The 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 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 ConcurrentHashMap class. 

Unlike Hashtable and 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.



To be frank, Collections classes are the heart of Java API though I feel using them judiciously is an art. It's my personal experience where I have improved the 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 a lack of scalability.


In multi-threaded Java application synchronized collection classes like Hashtable and Vector quickly becomes the bottleneck; to address scalability JDK 1.5 introduces some good concurrent collections which are highly efficient for high volume, low latency system electronic trading systems In general those are the backbone for Concurrent fast access to stored data.


In this tutorial, we will look on ConcurrentHashMap, Hashtable, HashMap and synchronized Map and see the 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 the multithreaded environment but once the size of Hashtable becomes considerable large performance degrade because for iteration it has to be locked for a longer duration.

Since ConcurrentHashMap introduced the 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 locks full map while doing iteration. This will be clearer by looking at this diagram which explains the internal working of ConcurrentHashMap in Java.

Difference between ConcurrentHashMap, Hashtable and Synchronized Map in Java




The 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 the common differences between ConcurrentHashMap and synchronized map in Java

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

Other Java Collection tutorials you may like
  • How to sort a Map by keys and values in Java? (tutorial)
  • How to sort an ArrayList in ascending and descending order in Java? (tutorial)
  • Difference between ArrayList and HashSet in Java? (answer)
  • The difference between TreeMap and TreeSet in Java? (answer)
  • The difference between HashMap and ConcurrentHashMap in Java? (answer)
  • The difference between HashMap and LinkedHashMap in Java? (answer)
  • The difference between Hashtable and HashMap in Java? (answer)
  • The difference between HashSet and TreeSet in Java? (answer)
  • The difference between ArrayList and LinkedList in Java? (answer)
  • The difference between Vector and ArrayList in Java? (answer)
  • Difference between EnumMap and HashMap in Java

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any question or feedback then please drop a comment.

48 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?

Unknown 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!!

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

Raj,

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.

Unknown 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(HashMapStructure.java:32)

Anonymous said...

how is partition done and how lock is done ?

Anonymous said...

Good article. Thanks.
In hashmap it will overwrite the values for NULL key. For ex: map.put(null, "abc"); map.put(null, "lmn");

Then last value will be there in hashmap with only one null key though you inserted N number of keys as null.

to get the null key value: map.get(null) will print "lmn" here.

javin paul said...

@Anonymous (November 25th), null keys are handled specifically in HashMap, they are stored at the first index in bucket array e.g. at index 0. So anytime, if you wan to retrieve value associated with null keys in HashMap it simply return table[0], it never call hashcode() method there to find the bucket location.

Regarding your second question, what will happen if you cast HashMap object to Hashtable as in your code, it will throw ClassCastException at runtime.

Anonymous said...

Hi. What do you mean by 'single collection-wide lock' in the part 'Why need ConcurrentHashMap and CopyOnWriteArrayList' of the article?

javin paul said...

@Anonymous, 'single collection wide lock' means whole collection will be locked when one thread does read, write or update. This means even if two threads are reading values, they will feel contention. This is optimized in ConcurrentHashMap and CopyOnWriteArrayList by using sophisticated technique of portion wide lock and copying the collection when change to give better performance and scalability.

Unknown said...

Hi,i was asked in an interview as default intial capacity of concurrent hash map is 16,so 16 threads can operate in parallel but what happens if there are more than 16 threads for write operation?

sharyu said...

Hi, could you suggest a book which will give the in detail information along with comparison of all the collections in java?

javin paul said...

@sharyu, read Java Generics and Collection, it's one of the top book in my list of must read Java books

Anonymous said...

Use synchronized block

julia said...

1st of all, hashTable and hashMap are different in nature of storing keys and data. Hashmap allows one null key and many null values, while hashTable doesn't allow any null key or value. So collections.synchronizedMap(new HashMap()) makes the hashmap only thread safe, rest of its feature remains same. In this respect, collections.synchronizedMap(new HashMap() and hashTable are not redundant feature.

Unknown said...

Hi,

I have one question, how do the ConcurrentHashMap handles resizing?
If more than 16 thread work on same ConcurrentHashMap , what will handle and how?

Unknown said...

Hi,

I have a question..
As you said, CHM is using stripped locking technique, there it locks certain segment while doing any change, So it means multiple threads not only read can also modify CHM concurrently.

Anonymous said...

Very neatly explained, very useful!

Osmand Prashant Das said...

Nice article!

Darpan said...

Thanks for writing all these articles. Very helping.

Indrani Sen said...

Very good explanation

Santhosh said...

Yes. It locks the entire map.

Srini said...

while segment is locked for write, whether read is allowed on that segment?

ayushOnJavaHibernate1 said...

Hi, I got the difference between concurrentHashMap and synchronized map. But Synchronized map is throwing concurrentModificationException as per example on https://www.javainuse.com/java/javaConcurrentHashMap
Then what is difference Between Normal HashMap and SynchronizedMap ??

Post a Comment