-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDictionary.java
More file actions
82 lines (73 loc) · 2.57 KB
/
Copy pathDictionary.java
File metadata and controls
82 lines (73 loc) · 2.57 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class Dictionary{
public static void main(String[] args){
final String[] words = {"hypocalcemia", "prolixity", "assiduous", "indefatigable", "castellan"};
final String[] definitions = {"a deficiency of calcium in the blood",
"unduly prolonged or drawn out",
"showing great care, attention, and effort",
"able to work or continue for a lengthy time without tiring",
"the govenor or warden of a castle or fort"};
final Map<String, String> dictionary = new HashMap<String, String>();
ReadWriteLock rwl = new ReentrantReadWriteLock(true);
final Lock writeLock = rwl.writeLock();
final Lock readLock = rwl.readLock();
System.out.println("WriteLock: " + writeLock + "; ReadLock: " + readLock);
Runnable writer = new Runnable(){
public void run(){
for(int i=0;i<words.length;i++){
writeLock.lock();
try{
System.out.println();
dictionary.put(words[i], definitions[i]);
System.out.println("writer storing " + words[i] + " entry"+ " from " + Thread.currentThread() + "; owning lock: " + writeLock);
System.out.println("writer doing something...");
System.out.println("writer doing something...");
System.out.println("writer doing something...");
}finally{
System.out.println("writer releasing the write lock" + "\n");
writeLock.unlock();
}
try{
Thread.sleep((int)(Math.random()*2000));
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
};
ExecutorService es = Executors.newFixedThreadPool(1);
es.submit(writer);
Runnable reader = new Runnable(){
public void run(){
while(true){
readLock.lock();
try{
int index = (int)(Math.random()*words.length);
System.out.println("reader accessing " + words[index] + ": " + dictionary.get(words[index]) + " entry" + " from " + Thread.currentThread() + "; owning lock: " + readLock);
}finally{
readLock.unlock();
}
try{
Thread.sleep((int)(Math.random()*500));
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
};
final int readerAmount = 5;
es = Executors.newFixedThreadPool(readerAmount);
for(int i=0;i<readerAmount; i++){
es.submit(reader);
}
}
}
/* Thread.toString() implementation.
* [ThreadName , ThreadPriority , ThreadGroupName]
*/