Understanding the internal working of ConcurrentHashMap in Java
When multiple threads access and modify a regular HashMap simultaneously, unexpected behavior can occur, including data corruption and…
Understanding the internal working of ConcurrentHashMap in Java
When multiple threads access and modify a regular HashMap simultaneously, unexpected behavior can occur, including data corruption and infinite loops. To solve this problem efficiently, Java provides ConcurrentHashMap, a thread-safe and high performance implementation of the Map interface.
What is ConcurrentHashMap ?
ConcurrentHashMap is a thread-safe hash table designed for concurrent access. Unlike HashTable, which locks the entire map for every operation, ConcurrentHashMap allows multiple threads to read and update the map simultaneously.
Example :
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
map.put("Apple", 1),
map.put("Orange, 2");
System.out.println(map.get("Apple");
Why not use HashTable ?
HashTable<String, Integer> table = new HashTable<>():
- Every operation is synchronized.
- Only one thread can access the map at a time.
- Performance decreases significantly under heavy concurrency.
ConcurrentHashMap
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();s
- Multiple threads can read concurrently.
- Writes lock only a samll portion of the structure.
- Better scalability and throughput.
Internal structure
Internally ConcurrentHashMap stores data in an array of buckets , similar to HashMap.
Table :
Bucket 0 -> Node -> Node
Bucket1 -> Node
Bucket 2 -> TreeNode
Bucket n
Each bucket contains:
- A linked list for smll number of collisions.
- A balanced red black tree when collisions becomes excessive.
Changes across Java versions :
Java 7 : Segment-Based Locking
Java 7 divided the map into multiple segments.
ConcurrentHashMap
Segment 1
Segment 2
Segment 3
Segment 16
Each segment maintains its own block.
Advantage : Multiple threads could modify different segments simultaneously.
Java 8 and later : Bucket-Level Synchronization
Instead of segments:
- Uses a single bucket array.
- Employs CAS(Compare-And-Swap) operations.
- Synchronizes only when necessary.
- Locks individual buckets during updates.
This significantly improves concurrency and reduces memory overhead.
Role of CAS(Compare-And-Swap)
CAS is a CPU-level atomic operation used heavily in modern concurrent programming.
This operation happens atomically without using a traditional lock.
Benefits :
- Reduces thread blocking.
- Better scalability.
- Higher throughput under contention.
When number of nodes in a bucket exceeds a threshold, the linked list is converted into a red-black tree.
Conclusions :
ConcurrentHashMap is one of the most important concurrent data structures in Java. It achieves thread safety without sacrificing performance by combining lock-free reads, bucket-level synchronization, CAS operations, and efficient collision handling.
While HashTable relies on a single lock for the entire map, ConcurrentHashMap allows a much higher degree of parallelism, making it the preferred choice for modern multi-threaded Java applications.
메타데이터
- post_id
- 19ba4e48ceb7
- slug
- understanding-the-internal-working-of-concurrenthashmap-in-java-19ba4e48ceb7
- url
- https://medium.com/@suryabhansingh16689/understanding-the-internal-working-of-concurrenthashmap-in-java-19ba4e48ceb7
- canonical_url
- https://medium.com/@suryabhansingh16689/understanding-the-internal-working-of-concurrenthashmap-in-java-19ba4e48ceb7
- author_url
- https://medium.com/@suryabhansingh16689
- status
- ok
- fetched_at
- 2026-08-10 06:06:41