Java Concurrent Map

Category: Java   Tags: Java, Java Thread, Java MultiThread, Thread Synchronization, Immutable Objects, Java Concurrent Collection

ConcurrentMap is a subinterface of java.util.Map supporting atomic operations and thread safety.

Methods

Here we are mentioning some important methods of this interface:

V putIfAbsent(K key, V value)
If the specified key exist then return the associated value other add the given key with given value. It returns previous value associated with given key or null if the given is not found.
boolean remove(Object key, Object value)
Removes the entry if given key is associated to the given value. Returns true if the value was removed.
V replace(K key, V value)
It will replace the entry if the given key is mapped to some value. It returns the previous value associated with the given key, or null if key doesn't exist in the map.
V replace(K key, V oldValue, V newValue)
It will replace the old value of given key with new value and returns true if the value was replaced.

So here is one example showing how ConcurrentMap works. This example use three thread to add, delete and display the content of a ConcurrentHashMap.

JavaConcurrentHashMapExample.java

                            package com.tutorial.javabasic;

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

                            class MyResource {

                                ConcurrentHashMap<Integer, Integer> map;

                                public MyResource() {
                                    map = new ConcurrentHashMap<>();
                                }

                                public void add(int key, int value) {
                                    map.putIfAbsent(key, value);
                                }

                                public void delete(int key) {
                                    map.remove(key);
                                    System.out.println("Elements after deletion the key:" + key);
                                    Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
                                    while (iterator.hasNext()) {
                                        System.out.println(iterator.next());
                                    }
                                }

                                public void view() {
                                    System.out.println("Display all elements:");
                                    Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
                                    while (iterator.hasNext()) {
                                        System.out.println(iterator.next());
                                    }
                                }

                            }

                            class ThreadOne implements Runnable {

                                private final MyResource resource;

                                ThreadOne(MyResource r) {
                                    resource = r;
                                }

                                public void run() {
                                    for (int i = 0; i < 10; i++) {
                                        resource.add(i, i);
                                    }
                                }
                            }

                            class ThreadTwo implements Runnable {

                                private final MyResource resource;

                                ThreadTwo(MyResource r) {
                                    resource = r;
                                }

                                public void run() {
                                    for (int i = 0; i < 10; i++) {
                                        resource.delete(i);
                                    }
                                }
                            }

                            class JavaConcurrentHashMapExample {

                                public static void main(String s[]) {
                                    MyResource resource = new MyResource();
                                    ThreadOne addThread = new ThreadOne(resource);
                                    ThreadTwo deleteThread = new ThreadTwo(resource);
                                    ThreadTwo displayThread = new ThreadTwo(resource);

                                    new Thread(addThread).start();
                                    new Thread(deleteThread).start();
                                    new Thread(displayThread).start();
                                }
                            }
                        

Output of this Java program is:

                            Elements after deletion the key:0
                            1=1
                            2=2
                            3=3
                            4=4
                            5=5
                            6=6
                            7=7
                            8=8
                            9=9
                            Elements after deletion the key:1
                            2=2
                            3=3
                            4=4
                            5=5
                            6=6
                            7=7
                            8=8
                            9=9
                            Elements after deletion the key:2
                            3=3
                            4=4
                            5=5
                            6=6
                            7=7
                            8=8
                            9=9
                            Elements after deletion the key:3
                            4=4
                            5=5
                            6=6
                            7=7
                            8=8
                            9=9
                            Elements after deletion the key:4
                            5=5
                            6=6
                            7=7
                            8=8
                            9=9
                            Elements after deletion the key:5
                            6=6
                            7=7
                            8=8
                            9=9
                            Elements after deletion the key:6
                            7=7
                            8=8
                            9=9
                            Elements after deletion the key:7
                            8=8
                            9=9
                            Elements after deletion the key:8
                            9=9
                            Elements after deletion the key:9
                            Elements after deletion the key:0
                            Elements after deletion the key:1
                            Elements after deletion the key:2
                            Elements after deletion the key:3
                            Elements after deletion the key:4
                            Elements after deletion the key:5
                            Elements after deletion the key:6
                            Elements after deletion the key:7
                            Elements after deletion the key:8
                            Elements after deletion the key:9
                          

In this Java program if you replace ConcurrentHashMap with HashMap you will get java.util.ConcurrentModificationException exception. This Java exception indicates that you can't modify HashMap in a multi thread environment and that is why ConcurrentHashMap is useful.