-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReaderWriter.java
More file actions
168 lines (141 loc) · 3.08 KB
/
ReaderWriter.java
File metadata and controls
168 lines (141 loc) · 3.08 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package OSP;
import java.util.concurrent.Semaphore;
public class ReaderWriter {
public static final int NUM_OF_READERS = 3;
public static final int NUM_OF_WRITERS = 2;
public static void main(String[] args) {
RWLock database = new Database();
Thread readerArray[] = new Thread[NUM_OF_READERS];
Thread writerArray[] = new Thread[NUM_OF_WRITERS];
for(int i = 0; i < NUM_OF_READERS; i++)
{
readerArray[i] = new Thread(new Reader(i, database));
readerArray[i].start();
}
for(int i = 0; i < NUM_OF_WRITERS; i++)
{
writerArray[i] = new Thread(new Writer(i, database));
writerArray[i].start();
}
}
}
interface RWLock
{
public abstract void acquireReadLock(int readerNum);
public abstract void acquireWriteLock(int writerNum);
public abstract void releaseReadLock(int readerNum);
public abstract void releaseWriteLock(int writerNum);
}
class Database implements RWLock
{
private int readerCount;
private Semaphore mutex;
private Semaphore db;
public Database()
{
readerCount = 0;
mutex = new Semaphore(1);
db = new Semaphore(1);
}
public void acquireReadLock(int readerNum)
{
try
{
mutex.acquire();
}
catch(InterruptedException e) {}
readerCount++;
if(readerCount == 1)
{
try
{
db.acquire();
}
catch(InterruptedException e) {}
}
System.out.println("Reader " + readerNum + " is reading. ReaderCount = " + readerCount);
mutex.release();
}
public void releaseReadLock(int readerNum)
{
try
{
mutex.acquire();
}
catch(InterruptedException e) {}
readerCount--;
if(readerCount == 0)
db.release();
System.out.println("Reader " + readerNum + " is done reading. ReaderCount = " + readerCount);
mutex.release();
}
public void acquireWriteLock(int writerNum)
{
try
{
db.acquire();
}
catch(InterruptedException e) {}
System.out.println("Writer " + writerNum + " is writng");
}
public void releaseWriteLock(int writerNum)
{
System.out.println("Writer " + writerNum + " is done writing");
db.release();
}
}
class Reader implements Runnable
{
private RWLock database;
private int readerNum;
public Reader(int rn, RWLock db)
{
readerNum = rn;
database = db;
}
public void run() {
while(true)
{
SleepUtilities.nap();
System.out.println("Reader " + readerNum + " wants to read");
database.acquireReadLock(readerNum);
SleepUtilities.nap();
database.releaseReadLock(readerNum);
}
}
}
class Writer implements Runnable
{
private RWLock database;
private int writerNum;
public Writer(int w, RWLock db)
{
writerNum = w;
database = db;
}
public void run()
{
while(true)
{
SleepUtilities.nap();
System.out.println("Writer " + writerNum + " wants to write");
database.acquireWriteLock(writerNum);
SleepUtilities.nap();
database.releaseWriteLock(writerNum);
}
}
}
class SleepUtilities
{
public static void nap()
{
nap(NAP_TIME);
}
public static void nap(int duration)
{
int sleeptime = (int) (NAP_TIME * Math.random() );
try { Thread.sleep(sleeptime*1000); }
catch (InterruptedException e) {}
}
private static final int NAP_TIME = 5;
}