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(); } } }