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.
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.
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.
ConcurrentHashMap does not allow null keys or null values while synchronized HashMap allows one null key.
Other Java Collection tutorials you may like
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.
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.
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.
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.
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 JavaConcurrentHashMap 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.
this article is not very clear.
ReplyDeleteCollections.synchronizedMap locks the entire map or not ?
Yes. It locks the entire map.
DeleteHi Michee, thanks for your comment. yes Collections.SynchronizedMap lock the entire Map while Concurrent Map only locks portion of Map.
ReplyDeleteAm I able to write in ordinal HashMap from different threads and after all threads complete read results from one thread?
ReplyDeleteHi 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.
ReplyDeleteConcurrentHashMap 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.
ReplyDeleteGood article.
ReplyDeleteThanks Anonymous , Glad you like this article on difference between ConcurrentHashMap and hashtable.
ReplyDeleteThanks 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
ReplyDeleteHi,
ReplyDeleteIn 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.
Deepthi,
ReplyDeleteBy using CopyOnWriteArrayList. Collections.synchronizedList will be better, but as explained in this article, CopyOnWriteArrayList will be your best bet.
- Sankha
Use synchronized block
Deletehi there! HashMap allows only one null key and many null values.(last stmt)
ReplyDeletenice site! congrats
@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.
ReplyDeleteHere 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?
ReplyDeleteHere 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?
ReplyDeleteSame Question? Is it just the scalibility issue of hashtable?
Hi,
ReplyDeleteI 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?
@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.
ReplyDeleteWhen 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?
ReplyDelete@Saurabh, ConcurrentHashMap use Segments Policy to add the objects in the Map.
ReplyDeleteIf 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
"... HashMap allows null keys."
ReplyDeleteHashMap allows atmost one null key, and there may be many null values.
I just went through few of your articles and they are really explaining things in a fine manner. Keep up the good work!!
ReplyDeleteCan someone post different questions on concurret collections concepts.
ReplyDeleteIt 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.
ReplyDeletein concurrenthashmap and synchronizedmp these two which one is better and why
ReplyDeleteHi I got below question in interview, can some one help on this?
ReplyDeletewhat 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?
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.
DeleteRaj,
ReplyDeleteBoth(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.
i have question ???
ReplyDeletewhose performance will be better ???
@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.
ReplyDeleteI was asked this question,
ReplyDeleteHashMap 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?
Hi ,
ReplyDeleteYou 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)
how is partition done and how lock is done ?
ReplyDeleteGood article. Thanks.
ReplyDeleteIn 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.
@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.
ReplyDeleteRegarding your second question, what will happen if you cast HashMap object to Hashtable as in your code, it will throw ClassCastException at runtime.
Hi. What do you mean by 'single collection-wide lock' in the part 'Why need ConcurrentHashMap and CopyOnWriteArrayList' of the article?
ReplyDelete@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.
ReplyDeleteHi,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?
ReplyDeleteHi, could you suggest a book which will give the in detail information along with comparison of all the collections in java?
ReplyDelete@sharyu, read Java Generics and Collection, it's one of the top book in my list of must read Java books
ReplyDeleteHi,
ReplyDeleteI have one question, how do the ConcurrentHashMap handles resizing?
If more than 16 thread work on same ConcurrentHashMap , what will handle and how?
Hi,
ReplyDeleteI 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.
Very neatly explained, very useful!
ReplyDeleteNice article!
ReplyDeleteThanks for writing all these articles. Very helping.
ReplyDeleteVery good explanation
ReplyDeletewhile segment is locked for write, whether read is allowed on that segment?
ReplyDeleteHi, 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
ReplyDeleteThen what is difference Between Normal HashMap and SynchronizedMap ??