Wednesday, 22 June 2011

Concurrent hashmap in java

ConcurrentHashMap extends the abstract class AbstractMap and implements the ConcurrentMap interface. This class follows the operational specification and the similar functional specification as of Hashtable, for updating a hash table supports the fully concurrency of the recoveries and adjustable expected concurrency. This class allows the variants of methods correspondence to each method of Hashtable and also doesn't permit the 'null' for the 'key' or 'value'. All the operations of this class are thread-safe. Use of ConcurrentHashMap increases the performance because it permits the multiple threads to modify the map concurrently without require to block them however performance may be poor if the single threads access the map at a time.

Syntax

public class ConcurrentHashMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>

Constructor's of ConcurrentHashMap

This class provides constructor through which we can create map according to our requirement :
ConcurrentHashMap() : This constructor makes a new vacate map of default size (16), default load factor (0.75) and concurrencyLevel (16).
ConcurrentHashMap(int initialCapacity) : This constructor makes a new vacate map of capacity defined at time of instantiation according to requirement with default load factor (0.75) and concurrencyLevel (16).
ConcurrentHashMap(int initialCapacity, float loadFactor) : This constructor makes a new vacate map of capacity and load factor defined at time of instantiation according to requirement and with default concurrencyLevel (16).
ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) : This constructor makes a new vacate map of capacity, load factor, and with concurrencyLevel defined at the time of instantiation according to requirement.
ConcurrentHashMap(Map<? extends K,? extends V> m) : This constructor makes the similar mappings according to the map given.

Methods of ConcurrentHashMap

This class provides methods some of the commonly used are :
  1. put()
  2.                         syntax : public V put(K key,V value)
  3. clear()
  4.                         syntax : public void clear()
  5. elements()
  6.                         syntax : public Enumeration elements()
  7. remove(Object key, Object value)
  8.                         syntax : public boolean remove(Object key,Object value)
  9. replace(K key, V value)
  10.                         syntax : public V replace(K key,V value)
  11. values()
  12.                         syntax : public Collection values()
  13. size()
  14.                         syntax : public int size()
Example :

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]



No comments:

Post a Comment