ConcurrentHashMap in Java

In Java, a HashMap is a popular implementation of the Map interface that allows us to store and retrieve key-value pairs. However, in multi-threaded applications, accessing a HashMap concurrently can lead to race conditions and thread-safety issues. To address this problem, Java provides the ConcurrentHashMap class, which is a thread-safe implementation of the Map interface. In this article, we will take a closer look at what ConcurrentHashMap is and how it works in Java.

What is ConcurrentHashMap?

ConcurrentHashMap is a thread-safe implementation of the Map interface in Java that allows multiple threads to access the map concurrently without any race conditions. It achieves this by partitioning the map into segments and only allowing one thread to modify a segment at a time.

Each segment of the ConcurrentHashMap is essentially a separate HashMap that is guarded by a ReentrantLock. When a thread wants to access the map, it first acquires the lock on the appropriate segment. This ensures that only one thread can modify that segment at a time. Other threads can still access the other segments concurrently.

How Does ConcurrentHashMap Work?

When we add a key-value pair to a ConcurrentHashMap, the hash code of the key is computed just like it is for a regular HashMap. However, instead of using a single global lock to protect the entire map, the ConcurrentHashMap uses a finer-grained locking mechanism.

The map is divided into a fixed number of segments, and each segment has its own lock. When we add or remove a key-value pair from the map, the lock for the corresponding segment is acquired. This ensures that only one thread can modify that segment at a time, but other threads can still access the other segments concurrently.

When we retrieve a value from a ConcurrentHashMap, the hash code of the key is computed again, and the segment that contains the key is determined. The lock for that segment is then acquired, and the value is retrieved from the corresponding HashMap.

ConcurrentHashMap Example

Here is an example of using a ConcurrentHashMap to store key-value pairs:

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapExample {
public static void main(String[] args) {
Map concurrentHashMap = new ConcurrentHashMap<>();
concurrentHashMap.put("One", 1);
concurrentHashMap.put("Two", 2);
concurrentHashMap.put("Three", 3);
concurrentHashMap.put("Four", 4);
System.out.println("ConcurrentHashMap: " + concurrentHashMap);

    int value = concurrentHashMap.get("Two");
    System.out.println("Value of key 'Two': " + value);
}
}


In this example, we create a ConcurrentHashMap and add four key-value pairs to it. We then retrieve the value associated with the key “Two” and print it.

The output of this program is as follows:

ConcurrentHashMap: {One=1, Two=2, Three=3, Four=4}
Value of key 'Two': 2

As we can see, the ConcurrentHashMap stores and retrieves key-value pairs correctly, even when multiple threads are accessing the map concurrently.

Conclusion

A ConcurrentHashMap in Java is a thread-safe implementation of the Map interface that allows multiple threads to access the map concurrently without any race conditions. By partitioning the map into segments and only allowing one thread to modify a segment at a time, the ConcurrentHashMap ensures that all modifications to the map are thread-safe. This makes it a useful data structure in multi-threaded applications where thread-safety is a concern.