-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoundedBuffer.java
More file actions
42 lines (39 loc) · 1.16 KB
/
Copy pathBoundedBuffer.java
File metadata and controls
42 lines (39 loc) · 1.16 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
public class BoundedBuffer {
private static final int BUFFER_SIZE = 7;
private final Semaphore mutex;
private final Semaphore empty;
private final Semaphore full;
public Integer[] buffer;
private int in, out;
private int count=0;
public BoundedBuffer() {
in = 0;
out = 0;
buffer = new Integer[BUFFER_SIZE];
mutex = new Semaphore(1);
empty = new Semaphore(BUFFER_SIZE);
full = new Semaphore(0);
}
public void insert() {
empty.acquire();
mutex.acquire();
buffer[in] = count;
// will put the item into the text filed
in = (in + 1) % BUFFER_SIZE;
System.out.println(Thread.currentThread().getName() + " has inserted " + count);
count++;
mutex.release();
full.release();
}
public void remove() {
full.acquire();
mutex.acquire();
Integer item;
// remove an item from the buffer
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
System.out.println(Thread.currentThread().getName() + " has removed " + item);
mutex.release();
empty.release();
}
}