What is ConcurrentHasMap and how do I use it in Java?

ConcurrentHashMap is a class in Java that implements the ConcurrentMap interface. It is part of the Java Collection Framework and extends the AbstractMap class.

ConcurrentHashMap is thread-safe, which means it is designed to support high concurrency levels by handling multiple threads concurrently without any inconsistencies. It allows multiple threads to perform retrieve (get) and update (insert & delete) operations. Internally, ConcurrentHashMap uses concepts of Segmentation to store data which allows higher degree of concurrency.

Here is an example of how to use ConcurrentHashMap in Java:

package org.kodejava.util;

import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapExample {
    public static void main(String[] args) {
        // Create a ConcurrentHashMap instance
        ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();

        // Add elements
        map.put("One", 1);
        map.put("Two", 2);
        map.put("Three", 3);

        // Retrieve elements
        Integer one = map.get("One");
        System.out.println("Retrieved value for 'One': " + one);

        // Remove an element
        map.remove("Two");

        // Print all elements
        map.forEach((key, value) -> System.out.println(key + " = " + value));
    }
}

Output:

Retrieved value for 'One': 1
One = 1
Three = 3

In this example, we’re creating a ConcurrentHashMap, adding some elements to it, retrieving an element, removing an element, and finally printing all the elements.

One thing to note is that while ConcurrentHashMap allows multiple threads to read and write concurrently, a get() operation might not reflect the latest put() operation, since it might be looking at a previous segment. Further thread synchronization mechanisms might be necessary depending on your exact use case.

Also, worth mentioning, null values and null keys are not permitted in ConcurrentHashMap to prevent ambiguities and potential errors in multithreaded contexts. If you try to use null, ConcurrentHashMap will throw a NullPointerException.

Here’s an example demonstrating the usage of ConcurrentHashMap in a multithreaded context:

package org.kodejava.util;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ConcurrentHashMapThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();

        // Create a ThreadPool with 5 threads
        try (ExecutorService executor = Executors.newFixedThreadPool(5)) {

            // Runnable task to increment a value in the map
            Runnable task = () -> {
                for (int i = 0; i < 10; i++) {
                    map.compute("TestKey", (key, value) -> {
                        if (value == null) {
                            return 1;
                        } else {
                            return value + 1;
                        }
                    });
                }
            };

            // Submit the task to each thread in the pool
            for (int i = 0; i < 5; i++) {
                executor.submit(task);
            }

            // Shut down the executor and wait for tasks to complete
            executor.shutdown();
            if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
                executor.shutdownNow();
            }
        }

        System.out.println("Final value for 'TestKey': " + map.get("TestKey"));
    }
}

Output:

Final value for 'TestKey': 50

In this example, we’re creating a ConcurrentHashMap and a thread pool with ExecutorService. We’re then defining a Runnable task, which increments the value of the “TestKey” key in the map 10 times.

The task uses ConcurrentHashMap‘s compute() method, which is atomic, meaning that the retrieval and update of the value is done as a single operation that cannot be interleaved with other operations. We then submit the task to each of the five threads in our thread pool. After all threads have completed their tasks, we retrieve and print the final value of “TestKey”.

If everything works correctly, the output should be “Final value for ‘TestKey’: 50”, because we have 5 threads each incrementing the value 10 times. This demonstrates the thread-safety of ConcurrentHashMap, as the compute() operation is done atomically and many threads were able to modify the map simultaneously without causing inconsistencies. If we were using a plain HashMap instead, we could not guarantee this would be the case.

How do I check whether a thread group has been destroyed?

You can use ThreadGroup.isDestroyed() method to check whether a thread group and its subgroups has been destroyed.

package org.kodejava.lang;

public class CheckGroupDestroy {
    public static void main(String[] args) {
        ThreadGroup grandParent = new ThreadGroup("GrandParent");
        ThreadGroup uncle = new ThreadGroup(grandParent, "Uncle");
        ThreadGroup parent = new ThreadGroup(grandParent, "Parent");
        ThreadGroup son = new ThreadGroup(parent, "Son");
        ThreadGroup daughter = new ThreadGroup(parent, "Daughter");
        ThreadGroup neighbour = new ThreadGroup("Neighbour");

        ThreadGroup[] groupArray = {
                grandParent, uncle, parent, son, daughter, neighbour
        };

        // Destroy 'parent' group and all its subgroups
        parent.destroy();

        // Check whether the group is destroyed. The result is,
        // GrandParent, Uncle, and Neighbour did not destroyed
        // because they are not Parent's subgroups
        for (ThreadGroup tg : groupArray) {
            if (tg.isDestroyed()) {
                System.out.println(tg.getName() + " is destroyed");
            } else {
                System.out.println(tg.getName() + " is not destroyed");
            }
        }
    }
}

The result is:

GrandParent is not destroyed
Uncle is not destroyed
Parent is destroyed
Son is destroyed
Daughter is destroyed
Neighbour is not destroyed

How do I destroy a thread group?

You can destroy a thread group by using destroy() method of ThreadGroup class. It will cleans up the thread group and removes it from the thread group hierarchy. It’s not only destroy the thread group, but also all its subgroups.

The destroy() method is of limited use: it can only be called if there are no threads presently in the thread group.

package org.kodejava.lang;

public class ThreadGroupDestroy {
    public static void main(String[] args) {
        ThreadGroup root = new ThreadGroup("Root");
        ThreadGroup server = new ThreadGroup(root, "ServerGroup");
        ThreadGroup client = new ThreadGroup(root, "ClientGroup");

        // Destroy 'root' thread groups and all its subgroup
        // ('server' & 'client')
        root.destroy();

        // Check if 'root' group and its subgroups already destroyed
        if (root.isDestroyed()) {
            System.out.println("Root group is destroyed");
        }

        if (server.isDestroyed()) {
            System.out.println("Server group is destroyed");
        }

        if (client.isDestroyed()) {
            System.out.println("Client group is destroyed");
        }
    }
}

How do I count active thread in a thread group?

This example describe how to get the number of active threads in this thread group. The result might not reflect concurrent activity, and might be affected by the presence of certain system threads.

Due to the inherently imprecise nature of the result, it is recommended that this method only be used for informational purposes.

package org.kodejava.lang;

public class ThreadGroupActiveThread {
    public static void main(String[] args) {
        ThreadGroup threadGroup = new ThreadGroup("TestThread");

        Thread t1 = new Thread(threadGroup, new Server(), "Server1");
        Thread t2 = new Thread(threadGroup, new Server(), "Server2");
        Thread t3 = new Thread(threadGroup, new Server(), "Server3");
        Thread t4 = new Thread(threadGroup, new Server(), "Server4");

        t1.start();
        t2.start();
        t3.start();
        t4.start();

        // Get an estimate number of active thread of the thread
        // group.
        int activeThread = threadGroup.activeCount();
        System.out.format("Number of active threads of %s is %d %n",
                threadGroup.getName(), activeThread);
    }
}

class Server implements Runnable {
    public void run() {
        System.out.println("Running..");
    }
}

How do I get number of active thread in current thread?

In this example you’ll see how to obtain the number of active thread in the current thread. You can use the Thread.activeCount() method to get this information.

package org.kodejava.lang;

public class CountActiveThread {
    public static void main(String[] args) {
        Thread t = new Thread(() -> System.out.println("Hello..."));
        t.start();

        // Get the number of active threads in the current thread's
        // thread group.
        int activeThread = Thread.activeCount();
        System.out.format("Number of active threads of %s is %d %n",
                Thread.currentThread().getName(), activeThread);
    }
}