-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReaderWriter.java
174 lines (156 loc) · 3.8 KB
/
ReaderWriter.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.io.*;
import java.util.*;
import java.util.concurrent.Semaphore;
import java.util.Arrays;
class ReaderInitiatorThread extends Thread
{
ReaderWriter obj;
ReaderInitiatorThread(ReaderWriter obj)
{
this.obj=obj;
}
public void run()
{
for(int i= 1;i<=obj.nr;i++)
{
Reader t = new Reader(i,obj);
t.start();
try{this.sleep(1);}catch(Exception e){System.out.println(e);}
}
}
}
class WriterInitiatorThread extends Thread
{
ReaderWriter obj;
WriterInitiatorThread(ReaderWriter obj)
{
this.obj = obj;
}
public void run()
{
for(int i= 1;i<=obj.wr;i++)
{
Writer t = new Writer(i,obj);
t.start();
}
}
}
class Reader extends Thread
{
ReaderWriter obj;
int x;
Thread t;
Reader(int x, ReaderWriter obj)
{
this.x=x;
this.obj=obj;
t = this.currentThread();
}
public void run()
{
try{
obj.reader.acquire();
obj.reader.release();
{System.out.println("Reader "+x+" reading from shared document");
obj.threads[x]=t;
};
}catch(Exception e){System.out.println(e);}
}
}
class Writer extends Thread
{
ReaderWriter obj;
int x;
Thread t;
Writer(int x, ReaderWriter obj)
{
this.x=x;
this.obj = obj;
t = this.currentThread();
}
public void run()
{
try{
//System.out.println(obj.reader.availablePermits());
{obj.reader.acquire(obj.reader.availablePermits());
obj.writer.acquire();
System.out.println("Suspending currently Reading Threads:");
for(int i=1;i<=obj.nr;i++)
{
if(obj.threads[i] == null )
continue;
obj.threads[i].suspend();
System.out.print(i+" ");
}
System.out.println();
System.out.println("Writer "+x+" is currently Writing");
System.out.println("Writer has finished writing");
System.out.println("Resuming Suspended Reader Threads:");
for(int i=1;i<=obj.nr;i++)
{
if(obj.threads[i] == null )
continue;
obj.threads[i].resume();
System.out.print(i+" ");
}
System.out.println();
obj.reader.release(obj.nr+1);
obj.writer.release();};
}catch(Exception e){System.out.println(e);}
}
}
public class ReaderWriter{
static Semaphore writer = new Semaphore(1);
static Semaphore reader = new Semaphore(0);
static int nr;
static int wr,rc=0;
static Thread [] threads = new Thread[1000001];
static Semaphore res = new Semaphore(1);
public static void main(String args[])
{
System.out.println("ReadersWriters Problem");
System.out.println("Enter the no. of readers");
Scanner sc = new Scanner(System.in);
nr=sc.nextInt();
Arrays.fill(threads,null);
reader.release(nr+1);
System.out.println("Enter the no. writers");
wr = sc.nextInt();
System.out.println("STARTING READER AND WRITER THREADS");
ReaderWriter obj = new ReaderWriter();
WriterInitiatorThread wit = new WriterInitiatorThread(obj);
ReaderInitiatorThread rit = new ReaderInitiatorThread(obj);
rit.start();
wit.start();
}
}
/* OUTPUT
*
*
ReadersWriters Problem
Enter the no. of readers
4
Enter the no. writers
3
STARTING READER AND WRITER THREADS
Suspending currently Reading Threads:
Writer 2 is currently Writing
Writer has finished writing
Resuming Suspended Reader Threads:
Suspending currently Reading Threads:
Reader 1 reading from shared document
Writer 1 is currently Writing
Writer has finished writing
Resuming Suspended Reader Threads:
1
Suspending currently Reading Threads:
1
Writer 3 is currently Writing
Writer has finished writing
Resuming Suspended Reader Threads:
Reader 2 reading from shared document
1 2
Reader 3 reading from shared document
Reader 4 reading from shared document
Process completed.
*/