← Back to list

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…

Surya Bhan Singh · 2026-06-02 11:32 · 0 claps · 1.5 min read
#concurrenthashmap #java
Open on Medium ↗

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<>():

  1. Every operation is synchronized.
  2. Only one thread can access the map at a time.
  3. Performance decreases significantly under heavy concurrency.

ConcurrentHashMap

ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();s
  1. Multiple threads can read concurrently.
  2. Writes lock only a samll portion of the structure.
  3. 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:

  1. A linked list for smll number of collisions.
  2. 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:

  1. Uses a single bucket array.
  2. Employs CAS(Compare-And-Swap) operations.
  3. Synchronizes only when necessary.
  4. 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 :

  1. Reduces thread blocking.
  2. Better scalability.
  3. 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