-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp07.java
88 lines (81 loc) · 2.29 KB
/
App07.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
public class App07 {
public static void main(String[] args) {
SharedBuffer buffer = new SharedBuffer(5);
Thread producerThread = new Thread(new Producer(buffer));
Thread consumerThread = new Thread(new Consumer(buffer));
producerThread.start();
consumerThread.start();
}
}
class SharedBuffer {
private int[] buffer;
private int size;
private int count;
public SharedBuffer(int size) {
this.size = size;
this.buffer = new int[size];
this.count = 0;
}
public synchronized void produce(int item) throws InterruptedException {
while (count == size) {
wait(); // Buffer is full, wait for the consumer to consume
}
buffer[count++] = item;
System.out.println("Produced: " + item);
notify(); // Notify the consumer that an item is produced
}
public synchronized int consume() throws InterruptedException {
while (count == 0) {
wait(); // Buffer is empty, wait for the producer to produce
}
int item = buffer[--count];
System.out.println("Consumed: " + item);
notify(); // Notify the producer that an item is consumed
return item;
}
}
class Producer implements Runnable {
private SharedBuffer buffer;
public Producer(SharedBuffer buffer) {
this.buffer = buffer;
}
@Override
public void run() {
try {
for (int i = 1; i <= 5; i++) {
buffer.produce(i);
Thread.sleep(1000); // Simulate production time
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Consumer implements Runnable {
private SharedBuffer buffer;
public Consumer(SharedBuffer buffer) {
this.buffer = buffer;
}
@Override
public void run() {
try {
for (int i = 0; i < 5; i++) {
int item = buffer.consume();
Thread.sleep(1500); // Simulate consumption time
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// output:
// Produced: 1
// Consumed: 1
// Produced: 2
// Consumed: 2
// Produced: 3
// Produced: 4
// Consumed: 4
// Produced: 5
// Consumed: 5
// Consumed: 3