ConcurrentHashMap Internals
Fast recall
Quick revision
Core answer
Depth of understanding beyond the surface-level 'it uses segments' answer (which was Java 7). Interviewers want Java 8+ knowledge.
Saying ConcurrentHashMap uses 16 segments — that was the Java 7 design. Java 8 replaced segments with per-bucket synchronisation.
Walk through the three phases of a put() operation: CAS on empty bucket, synchronized on head node for collision, and tree conversion at TREEIFY_THRESHOLD=8.
Interview-ready guide
Interview answer
Java 8 completely rewrote ConcurrentHashMap — gone are the 16 segments; the new design achieves lock-free reads with per-bucket writes.
Write strategy:
- Empty bucket → CAS atomically places the first node — no lock needed
- Non-empty bucket →
synchronizedon the first node of that bucket only; all other buckets are completely unaffected
Read strategy:
- Completely lock-free — node values are volatile;
get()traverses the chain with no locking - Read performance matches an unsynchronized HashMap
size() approximation:
- LongAdder-style counting: base counter with CAS, overflows into per-thread cells under contention
- Fast but approximate during concurrent modifications —
mappingCount()for long result
Null prohibition:
- No null keys or values —
get()returning null must unambiguously mean key-absent - In concurrent code, you can't safely call
containsKey()thenget()— another thread can remove between the two calls
I recommend computeIfAbsent() and merge() for compound operations — they execute atomically within the bucket lock, replacing the broken check-then-act pattern.
ConcurrentHashMap Internals
Direct Answer
Java 8+ ConcurrentHashMap uses a lock-striping approach where each bucket (array slot) has its own synchronised block, and reads are entirely lock-free using volatile array reads. Updates acquire a lock only on the affected bucket, so concurrent writes to different buckets proceed in parallel. The map also uses a CAS instruction to initialise empty buckets without locking.
Common Mistakes & Pitfalls
Saying ConcurrentHashMap uses 16 segments — that was the Java 7 design. Java 8 replaced segments with per-bucket synchronisation.
Check your understanding
Answer these without looking back at the article.
- Why is ConcurrentHashMap not suitable for atomic multi-key operations?
- What is the difference between ConcurrentHashMap and Collections.synchronizedMap()?
- How does the treeification of buckets protect against hash-flooding attacks?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.