-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentHashMapDemo2.java
More file actions
36 lines (29 loc) · 911 Bytes
/
ConcurrentHashMapDemo2.java
File metadata and controls
36 lines (29 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
class ConcurrentHashMapDemo2 extends Thread
{
static ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
public void run()
{
try { Thread.sleep(2000); }
catch(InterruptedException ie) {}
System.out.println("Child Thread updating Map");
concurrentHashMap.put(103, "C");
}
public static void main(String[] args) throws InterruptedException
{
concurrentHashMap.put(101, "A");
concurrentHashMap.put(102, "B");
ConcurrentHashMapDemo2 thread = new ConcurrentHashMapDemo2();
thread.start();
Set set = concurrentHashMap.keySet();
Iterator itr = set.iterator();
while(itr.hasNext())
{
Integer val = (Integer) itr.next();
System.out.println("Main Thread iterating and current entry is: "+val+" = "+concurrentHashMap.get(val));
Thread.sleep(3000);
}
System.out.println(concurrentHashMap);
}
}