Tutorials  /  JavaScript

ConcurrentHashMap: Thread Safety and Flexibility in Java

Ccentron Redaktion · October 2024 ·3 min read ·JavaScript, Tutorial

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.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

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:

Go
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:

Code
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:

Code
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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren