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

18 comments:
this article is not very clear.
Collections.synchronizedMap locks the entire map or not ?
Hi Michee, thanks for your comment. yes Collections.SynchronizedMap lock the entire Map while Concurrent Map only locks portion of Map.
Am I able to write in ordinal HashMap from different threads and after all threads complete read results from one thread?
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.
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.
Good article.
Thanks Anonymous , Glad you like this article on difference between ConcurrentHashMap and hashtable.
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
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.
Deepthi,
By using CopyOnWriteArrayList. Collections.synchronizedList will be better, but as explained in this article, CopyOnWriteArrayList will be your best bet.
- Sankha
hi there! HashMap allows only one null key and many null values.(last stmt)
nice 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.
Thanks Javin, very informative blog.
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?
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?
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?
@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.
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?
Post a Comment