Concurrency is one of the main features in java. By concurrency, we mean that the application/object/class supports concurrent access which means that multi-threading is possible.
Hashmap in java,
as we have already seen, are not thread-safe and concurrent. So, to introduce
concurrency with a hashmap, the ConcurrentHashmap came into the picture.
ConcurrentHashmap
was introduced in Java 1.5. ConcurrentHashmap implements ConcurrentMap and
Serializable interface.
Before moving
further let’s discuss some of the key points related to ConcurrentHashmap:
Key points of ConcurrentHashmap
·
Hashtable is the underlining Data structure of ConcurrentHashmap.
·
ConcurrentHashmap
is thread-safe, hence multiple threads can work on the object.
·
In
ConcurrentHashmap, according to the concurrency level, the object is divided
into segments.
·
Concurrency
level defines the number which is an estimated number of threads updated at the
same time. The implementation performs internal sizing to try to accommodate
this number of threads.
·
Default
concurrency level is 16.
·
No
null key or value is allowed in ConcurrentHashmap.
Difference between Hashmap and ConcurrentHashmap
·
Hashmap
is not thread-safe whereas ConcurrentHashmap is thread-safe.
·
As
Hashmap is non-synchronised the performance is relatively higher whereas ConcurrentHashmap
is relatively lower in performance as at some point of time the threads need to
wait for the turn.
·
While
iterating the Hashmap if we try to modify the state of the Object then JVM will
throw ConcurrentModificationException
whereas in ConcurrentHashmap we can do modification while iterating also.
Implementation of ConcurrentHashmap in Java
The ConcurrentHashmap
logic is based on Segments. The ConcurrentHashmap will be partitioned into
Segments based on Concurrency level and during an update, only the part of the ConcurrentHashmap
will be locked.
The concept of partial
locking is not available if we use a normal map with Collections.Synchronised like
Collections.Synchronised(map).
As we already
know that the default concurrency level is 16, so the map will be divided
into16 parts and each part will have its lock. 16 threads can operate simultaneously
on the map.
While using putall() or clear(), working on the whole map, the inclusion and removal of
only a few entries in the concurrent reading may be reflected. Another
important point to keep in mind is that the iterations on ConcurrentHashmap are
consistent weekly through the Iterator returned by KeySet of Concurrent Hashmap and they only reflect the state of the
Concurrent Hashmap and the specific point and may not reflect any recent
changes. ConcurrentHashmap's iterator is fail-safe and does not contain ConcurrentModificationException.
putIfAbsent()
in ConcurrentHashmap:
While using
normal hashmap sometimes we come across a condition where we need to make entries
if the value is not present, to do so we write the following code :
Although this code will work fine with Hashmap and Hashtable, it will not work with ConcurrentHashmap because in ConcurrentHashmap during put() whole ConcurrentHashmap will not be locked. And also while putting the values into the ConcurrentHashmap the other thread can read the value.
In this code, we are using Synchronised block, which is making the object as single-threaded. To avoid this race condition ConcurrentHashmap has putIfAbsent(key, value) method.
Example of ConcurrentHashmap
Related Articles:
1. Core java interview questions.
2. Java 8 interview questions.
3. Internal working of Hashmap in java.
0 Comments