Thursday 20 August 2015

HashMap Vs ConcurrentHashMap

HashMap Vs ConcurrentHashMap


Thread -Safe

ConcurrentHashMap is thread-safe that is the code can be accessed by single thread at a time.

HashMap is not thread-safe.

Synchronization Method

ConcurrentHashMap synchronizes or locks on the certain portion of the Map.

To optimize the performance of ConcurrentHashMap, Map is divided into different partitions depending upon the Concurrency level.

So that we do not need to synchronize the whole Map Object.

HashMap can be synchronized by using synchronizedMap(HashMap) method.

By using this method we get a HashMap object which is equivalent to the HashTable object.

So every modification is performed on Map is locked on Map object.

Null Key
ConcurrentHashMap does not allow NULL values i.e. key cannot be null.

While In HashMap there can only be one null key.

Performance 

In multiple threaded environments HashMap is usually faster than ConcurrentHashMap.

As only single thread can access the certain portion of the Map and thus reducing the performance.

Because any number of threads can access the HashMap at the same time.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...