-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoundedBuffer.java
50 lines (47 loc) · 1.71 KB
/
BoundedBuffer.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
package precodes;
class BoundedBuffer { // designed for a single producer thread
// and a single consumer thread
private int numSlots = 0;
private double[] buffer = null;
private int putIn = 0, takeOut = 0;
private int count = 0;
public BoundedBuffer(int numSlots) {
if (numSlots <= 0) throw new IllegalArgumentException("numSlots<=0");
this.numSlots = numSlots;
buffer = new double[numSlots];
System.out.println("BoundedBuffer alive, numSlots=" + numSlots);
}
public synchronized void deposit(double value) {
while (count == numSlots)
try {
wait();
} catch (InterruptedException e) {
System.err.println("interrupted out of wait");
}
buffer[putIn] = value;
putIn = (putIn + 1) % numSlots;
count++; // wake up the consumer
if (count == 1) notify(); // since it might be waiting
System.out.println(" after deposit, count=" + count
+ ", putIn=" + putIn);
}
public synchronized double fetch() {
double value;
while (count == 0)
try {
wait();
} catch (InterruptedException e) {
System.err.println("interrupted out of wait");
}
value = buffer[takeOut];
takeOut = (takeOut + 1) % numSlots;
count--; // wake up the producer
if (count == numSlots-1) notify(); // since it might be waiting
System.out.println(" after fetch, count=" + count
+ ", takeOut=" + takeOut);
return value;
}
public static void main(String args[]) {
BoundedBuffer b=new BoundedBuffer(3);
}
}