Concurrent Collections Interview Questions & Answers
This guide explains why Java 1.5 introduced concurrent collections and dives deep into the internal working of ConcurrentHashMap.
1. Why were Concurrent Collections introduced?
Traditional collections like HashMap and ArrayList are not thread-safe. While Hashtable and Vector are thread-safe, they have several issues:
- Performance: They lock the entire collection for every operation, causing significant delays in multi-threaded environments.
- ConcurrentModificationException: If one thread is iterating over a collection while another modifies it structurally (adding/removing), a
ConcurrentModificationExceptionis thrown.
Solution: Concurrent collections (like ConcurrentHashMap) allow multiple threads to operate simultaneously without throwing exceptions or locking the entire object.
2. HashMap vs. ConcurrentHashMap (Code Demo)
In a standard HashMap, adding an element during iteration causes a crash:
// Throws ConcurrentModificationException
map.keySet().iterator();
map.put(4, 4);
In ConcurrentHashMap, this is perfectly legal and will not throw an exception.
3. How does ConcurrentHashMap achieve better performance?
Unlike Hashtable, which locks the whole map, ConcurrentHashMap uses Lock Stripping (Segment Locking).
- Segments: The map is divided into multiple segments (default is 16).
- Independent Locking: A thread only acquires a lock on the specific segment it is writing to. Other threads can still read from or write to different segments simultaneously.
4. What is Concurrency Level?
The concurrency level is an integer that determines the number of estimated simultaneously updating threads.
- By default, it is 16.
- This value dictates the number of internal segments. If you have 16 segments, 16 different threads can write to 16 different buckets at the same time without blocking each other.
5. Why are null keys/values not allowed in ConcurrentHashMap?
ConcurrentHashMap does not allow null to avoid ambiguity in multi-threaded environments.
- In a single-threaded
HashMap,get(key) == nullcould mean the value isnullOR the key doesn't exist. - In a multi-threaded
ConcurrentHashMap, ifget(key)returnsnull, you wouldn't know if the value was alwaysnullor if another thread deleted the key between yourcontainsKey()check and yourget()call.
6. Internal Operations: Get vs. Put
- Read (Get): Generally non-blocking. Multiple threads can read from the same or different segments at the same time.
- Write (Put): Blocking at the segment level.
- Calculate the Segment Index.
- Calculate the Hash/Bucket Index.
- Acquire a lock on the specific segment.
- Perform the update.
7. What is "Lock Stripping"?
Lock stripping is the technique where a large data structure is broken into smaller pieces (strips), each with its own lock. This allows high levels of concurrency because threads only compete for locks if they are accessing the same "strip."