-
Notifications
You must be signed in to change notification settings - Fork 0
/
testReadWrite.java
54 lines (46 loc) · 1.52 KB
/
testReadWrite.java
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
public class testReadWrite {
final ThreadSynch gate;
public testReadWrite(ThreadSynch gate) {
this.gate = gate;
}
public static void main(String[] args) {
Thread[] readers = new Thread[10];
Thread[] writers = new Thread[10];
FairReadWriteLock rwl = new FairReadWriteLock();
// for(int i = 0; i < readers.length; i++) { //Option to create readers separately from writers
// readers[i] = new Thread(new Reader());
// readers[i].start();
// }
//
// for(int i = 0; i < writers.length; i++) {
// writers[i] = new Thread(new Writer());
// writers[i].start();
// }
for(int i = 0; i < readers.length; i++) { //Option to create readers and writers approximately at the same time
readers[i] = new Thread(new Reader(rwl));
writers[i] = new Thread(new Writer(rwl));
readers[i].start();
writers[i].start();
}
}
}
class Reader implements Runnable {
FairReadWriteLock rwl;
public Reader(FairReadWriteLock rwl) {
this.rwl = rwl;
}
public void run() {
// System.out.println("Beginning read on Thread " + Thread.currentThread().getId());
rwl.beginRead();
}
}
class Writer implements Runnable {
FairReadWriteLock rwl;
public Writer(FairReadWriteLock rwl) {
this.rwl = rwl;
}
public void run() {
// System.out.println("Beginning write on Thread " + Thread.currentThread().getId());
rwl.beginWrite();
}
}