Java Collection Framework - ConcurrentMap<k,v> Interface

Java Collection Framework - ConcurrentMap Interface


Posted in : Core Java Posted on : March 10, 2011 at 7:22 PM Comments : [ 0 ]

In java, concurrent collections are a good manner to avoid high levels of contention in multithreaded programs. A ConcurrentMap is a sub interface of the Map interface, that provides some additional atomic methods for inserting, replacing and removing the elements.

Java Collection Framework - ConcurrentMap<k,v> Interface

In java, concurrent collections are a good manner to avoid high levels of contention in multithreaded programs. A ConcurrentMap is a sub interface of the Map interface, that provides some additional atomic methods for inserting, replacing and removing the elements.

ConcurrentMap implements the thread safe implementation of Map which permits for concurrent modification from various threads, for this there is no need to block them. This interface gives the better performance when the map is accessed concurrently by the multiple threads as compare to only single thread accesses the map.      

Memory consistency effects : With the other concurrent collections take an actions into a thread, prior to allotting an object as a key or value actions into a ConcurrentMap to later access or removal of that object from the ConcurrentMap in other thread.

Parameter description

k : It is the key's type held by the map.

v : The type of value associated by key.

The list of methods supported by ConcurrentMap interface are given below :

Here is the example for demonstration of the ConcurrentMap interface :-

package devmanuals.com;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Set;
class A implements Runnable {
  String name;
  ConcurrentMap cm;
  public A(ConcurrentMap cm, String name) {
    this.name = name;
    this.cm = cm;
  }
  public void run() {
    try {
      cm.put(1, "A");
      cm.put(2, "B");
      cm.put(3, "C");
      cm.put(4, "D");
      cm.put(5, "E");
      System.out.println(name + " maps the element : " + cm);
      System.out.println(name + " represents the set of keys: "
          + cm.keySet());
      Thread.sleep(500);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
class B implements Runnable {
  String name;
  ConcurrentMap cm;
  public B(ConcurrentMap cm, String name) {
    this.name = name;
    this.cm = cm;
  }
 public void run() {
    try {
      boolean j = cm.remove(3, "C");
      System.out.println(name + " removes the element : " + j);
      Thread.sleep(500);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
class C implements Runnable {
  String name;
  ConcurrentMap cm;
  public C(ConcurrentMap cm, String name) {
    this.name = name;
    this.cm = cm;
  }
 public void run() {
    try {
      Set s = cm.keySet();
      System.out.println(name + " represents 
                                  the set of keys : " + s);
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
public class ConcurrentMapDemo {
  public static void main(String[] args) {
    ConcurrentMap cm = 
                          new ConcurrentHashMap();
    Runnable a = new A(cm, "A");
    Runnable b = new B(cm, "B");
    Runnable c = new C(cm, "C");
    new Thread(a).start();
    try {
      Thread.sleep(300);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    new Thread(b).start();
    try {
      Thread.sleep(300);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    new Thread(c).start();
    try {
      Thread.sleep(300);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

Output :

A maps the element : {5=E, 4=D, 3=C, 2=B, 1=A}

A represents the set of keys: [5, 4, 3, 2, 1]

B removes the element : true

C represents the set of keys : [5, 4, 2, 1]

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics