ConcurrentHashMap: Thread Safety and Flexibility in Java

The class ConcurrentHashMap belongs to the Concurrency Collection Classes in Java and provides an effective solution for handling multiple accesses in a multithreaded environment. It is based on a hash table implementation that supports concurrent read and write operations without the risk of a ConcurrentModificationException. This flexibility is crucial for the development of modern applications that have high performance requirements.

What is a ConcurrentHashMap?

A common problem in Java arises when a collection is modified during iteration. This leads to a ConcurrentModificationException. To address this challenge, concurrent classes were introduced in the java.util.concurrent package starting with Java 1.5. The ConcurrentHashMap allows modification of the map during iteration and guarantees that the operations are thread-safe. It is important to note that ConcurrentHashMap does not allow null values for keys and values. This ensures that the integrity of the data is maintained throughout the runtime.

Example of a ConcurrentHashMap in Java

The ConcurrentHashMap is similar to the HashMap, but with the significant advantage of thread safety and the ability to modify during iteration. These features make it an ideal candidate for storing data in applications that have high demands for concurrency.

Here is a simple example for illustration:

 
package com.journaldev.util;

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

public class ConcurrentHashMapExample {

    public static void main(String[] args) {
        // ConcurrentHashMap
        Map<String, String> myMap = new ConcurrentHashMap<String, String>();
        myMap.put("1", "1");
        myMap.put("2", "1");
        myMap.put("3", "1");
        myMap.put("4", "1");
        myMap.put("5", "1");
        myMap.put("6", "1");
        
        System.out.println("ConcurrentHashMap before iterator: " + myMap);
        Iterator it = myMap.keySet().iterator();

        while (it.hasNext()) {
            String key = it.next();
            if (key.equals("3")) myMap.put(key + "new", "new3");
        }
        System.out.println("ConcurrentHashMap after iterator: " + myMap);

        // HashMap
        myMap = new HashMap<String, String>();
        myMap.put("1", "1");
        myMap.put("2", "1");
        myMap.put("3", "1");
        myMap.put("4", "1");
        myMap.put("5", "1");
        myMap.put("6", "1");
        
        System.out.println("HashMap before iterator: " + myMap);
        Iterator it1 = myMap.keySet().iterator();

        while (it1.hasNext()) {
            String key = it1.next();
            if (key.equals("3")) myMap.put(key + "new", "new3");
        }
        System.out.println("HashMap after iterator: " + myMap);
    }
}

Output and Error Analysis

The output of the above code shows that the ConcurrentHashMap successfully adds a new entry during iteration, while the HashMap throws a ConcurrentModificationException.

Output:

 
ConcurrentHashMap before iterator: {1=1, 5=1, 6=1, 3=1, 4=1, 2=1}
ConcurrentHashMap after iterator: {1=1, 3new=new3, 5=1, 6=1, 3=1, 4=1, 2=1}
HashMap before iterator: {3=1, 2=1, 1=1, 6=1, 5=1, 4=1}
Exception in thread "main" java.util.ConcurrentModificationException

The ConcurrentModificationException occurs because the iterator responds to the structural change of the HashMap that occurred during iteration. This exception highlights the importance of understanding how collections work in Java.

Handling Modifications During Iteration

If we modify the code to break out of the iteration when inserting a new entry, we can avoid the exception:

 
if (key.equals("3")) {
    myMap.put(key + "new", "new3");
    break;  // Break the loop
}

Conclusion

The ConcurrentHashMap is a powerful and flexible solution for multithreaded environments in Java. It allows modifications during iteration without exceptions, making it ideal for applications that require high performance and security. Additionally, it is noteworthy that the use of generics when creating collection objects in Java not only improves type checking at compile time but also helps prevent ClassCastException at runtime. These advantages make the ConcurrentHashMap an indispensable tool for developers.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: