Showing posts with label ConcurrentHashMap. Show all posts
Showing posts with label ConcurrentHashMap. Show all posts

Friday, 7 October 2016

How does ConcurrentHashMap achieves its Scalability?

Question) How does ConcurrentHashMap achieve its Scalability?
Sometimes this multithreading + collection interview question is also asked as, the difference between ConcurrentHashMap and Hashtable in Java.

The problem with synchronized HashMap or Hashtable was that the whole Map is locked when a thread performs any operation with Map.


The java.util.ConcurrentHashMap class solves this problem by using the lock stripping technique, where the whole map is locked at different segments and only a particular segment is locked during the write operation, not the whole map.

The ConcurrentHashMap also achieves its scalability by allowing lock-free reads as read is a thread-safe operation.