-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.java
More file actions
67 lines (58 loc) · 2.24 KB
/
Buffer.java
File metadata and controls
67 lines (58 loc) · 2.24 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
/**
* The `Buffer` class represents a bounded buffer with a fixed capacity.
* It provides methods for producers to add items to the buffer and
* consumers to remove items from the buffer. The buffer uses a blocking
* queue to ensure thread safety. Producers can produce items and add
* them to the buffer, while consumers can consume items from the buffer.
* The class also keeps track of the number of producers finished and items
* produced to determine when all items have been produced and consumed.
**/
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Buffer {
private BlockingQueue<Integer> buffer;
private int producersFinished;
private int itemsProduced;
public Buffer(int capacity) {
this.buffer = new ArrayBlockingQueue<>(capacity);
this.producersFinished = 0;
this.itemsProduced = 0;
}
// The produce method is used by producers to add items to the buffer.
public void produce(long producerID, int itemCount) throws InterruptedException {
for (int i = 0; i < itemCount; i++) {
int item = i + 1;
buffer.put(item);
itemsProduced++;
System.out.println("Producer " + producerID + " produced item: " + item);
Thread.sleep(500);
}
synchronized (this) {
producersFinished++;
if (producersFinished == itemCount) {
notifyAll();
}
}
}
// Consume items from the buffer by the consumer
public void consume(long consumerID, int itemCount) throws InterruptedException {
for (int i = 0; i < itemCount; i++) {
synchronized (this) {
while (buffer.isEmpty() && producersFinished < itemCount) {
wait();
}
if (producersFinished == itemCount && buffer.isEmpty()) {
break;
}
int item = buffer.take();
System.out.println("Consumer " + consumerID + " consumed item: " + item);
Thread.sleep(500);
}
}
synchronized (this) {
if (itemsProduced == itemCount) {
notifyAll();
}
}
}
}