Repository navigation
Expand file tree
/
Copy pathCircleBuffer.java
More file actions
135 lines (119 loc) · 3.74 KB
/
Copy pathCircleBuffer.java
File metadata and controls
135 lines (119 loc) · 3.74 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
/*
* There are multiple producer processes and multiple consumer processes in the system,
* sharing a ring buffer of 1000 products, which is initially empty.
* When the buffer is full, the producer process can put in one of its products,
* otherwise wait;
* When the buffer is not empty, the consumer process can remove a product from the buffer,
* otherwise wait.
* A consumer process is required to take 10 products from the buffer in a row
* before other consumer processes can take products.
* Implements synchronization between processes.
*/
public class CircleBuffer {
public static void main(String[] args) {
Semaphores semaphores = new Semaphores();
Thread producer1 = new Thread(new Producer(semaphores, 1));
Thread producer2 = new Thread(new Producer(semaphores, 2));
Thread consumer1 = new Thread(new Consumer(semaphores, 1));
Thread consumer2 = new Thread(new Consumer(semaphores, 2));
producer1.start();
producer2.start();
consumer1.start();
consumer2.start();
}
}
class Semaphores {
public boolean mutex1 = true;
public boolean mutex2 = true;
public int empty = 1000;
public int full = 0;
public synchronized boolean pMutex1() {
if (mutex1) {
mutex1 = false;
return true;
} else {
return false;
}
}
public synchronized boolean pMutex2() {
if (mutex2) {
mutex2 = false;
return true;
} else {
return false;
}
}
public synchronized boolean pEmpty() {
if (empty > 0) {
empty--;
return true;
} else {
return false;
}
}
public synchronized boolean pFull() {
if (full > 0) {
full--;
return true;
} else {
return false;
}
}
public synchronized void vMutex1() {
mutex1 = true;
}
public synchronized void vMutex2() {
mutex2 = true;
}
public synchronized void vEmpty() {
empty++;
}
public synchronized void vFull() {
full++;
}
}
class Producer implements Runnable {
public Semaphores semaphores;
public int rank;
public Producer(Semaphores semaphores, int rank) {
this.semaphores = semaphores;
this.rank = rank;
}
public void run() {
while (true) {
System.out.println("Producer " + rank + " produces a production.");
while (!semaphores.pEmpty()) {
}
while (!semaphores.pMutex2()) {
}
System.out.println("Producer " + rank + " puts a production into the buffer.");
semaphores.vMutex2();
semaphores.vFull();
}
}
}
class Consumer implements Runnable {
public Semaphores semaphores;
public int rank;
public Consumer(Semaphores semaphores, int rank) {
this.semaphores = semaphores;
this.rank = rank;
}
public void run() {
while (true) {
while (!semaphores.pMutex1()) {
}
for (int i = 1; i <= 10; i++) {
while (!semaphores.pFull()) {
}
while (!semaphores.pMutex2()) {
}
System.out.println("Consumer " + rank + " gets the " + i + "st/nd/rd/th production from the buffer.");
semaphores.vMutex2();
semaphores.vEmpty();
System.out.println("Consumer " + rank + " consumes the " + i + "st/nd/rd/th production.");
}
semaphores.vMutex1();
}
}
}