-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
113 lines (95 loc) · 3.59 KB
/
Test.java
File metadata and controls
113 lines (95 loc) · 3.59 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
class ReadWriteLock{
static Semaphore enter = new Semaphore(1);
static Semaphore enter2 = new Semaphore(1);
static Semaphore writer = new Semaphore(0);
int s_reader = 0;
int c_reader = 0;
boolean w_writer = false;
public void readLock() throws InterruptedException {
enter.acquire();
s_reader++;
enter.release();
}
public void readUnLock() throws InterruptedException {
enter2.acquire();
c_reader++;
if(w_writer && s_reader == c_reader) {
writer.release();
}
enter2.release();
}
public void writeLock() throws InterruptedException {
enter.acquire();
enter2.acquire();
if(s_reader == c_reader){
enter2.release();
}else{
w_writer = true;
enter2.release();
writer.acquire();
w_writer = false;
}
}
public void writeUnLock() throws InterruptedException {
enter.release();
}
};
class Writer implements Runnable {
private ReadWriteLock lock;
public Writer(ReadWriteLock rw) {
lock = rw;
}
public void run() {
while (true){
try {
lock.writeLock();
System.out.println("Thread "+Thread.currentThread().getName() + " is WRITING"); //critical section
Thread.sleep(2000); //critical section
System.out.println("Thread "+Thread.currentThread().getName() + " has finished WRITING"); //critical section
lock.writeUnLock();
Thread.sleep(1000); //out of critical section
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Reader implements Runnable {
private sync.ReadWriteLock lock;
public Reader(sync.ReadWriteLock rw) { lock = rw; }
public void run() {
while (true) {
try {
lock.readLock();
System.out.println("Thread "+Thread.currentThread().getName() + " is READING"); //critical section
Thread.sleep(2000); //critical section
System.out.println("Thread "+Thread.currentThread().getName() + " has FINISHED READING"); //critical section
lock.readUnLock();
Thread.sleep(1000); //out of critical section
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String [] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
ReadWriteLock RW = new ReadWriteLock();
executorService.execute(new Reader(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Writer(RW));
executorService.shutdown();
}
}