-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProducerConsumer.java
More file actions
123 lines (103 loc) · 3.24 KB
/
ProducerConsumer.java
File metadata and controls
123 lines (103 loc) · 3.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
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
public class ProducerConsumer {
public static void main(String[] args) {
System.out.println("Producer Consumer Problem");
System.out.println("-----------------------");
// Create shared buffer
Buffer buffer = new Buffer();
// Create producer and consumer threads
Thread producerThread = new Thread(new Producer(buffer));
Thread consumerThread = new Thread(new Consumer(buffer));
// Start threads
producerThread.start();
consumerThread.start();
// Let the threads run for a while
try {
Thread.sleep(10000); // Run for 10 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
// Interrupt the threads to stop them
producerThread.interrupt();
consumerThread.interrupt();
System.out.println("Main thread exiting.");
}
}
// Shared buffer class
class Buffer {
private int data;
private boolean available = false;
// Method for producer to put data
public synchronized void put(int value) {
// Wait until consumer consumes the previous value
while (available) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
// Produce data
data = value;
available = true;
System.out.println("Producer produced: " + value);
// Notify consumer
notify();
}
// Method for consumer to get data
public synchronized int get() {
// Wait until producer puts a value
while (!available) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return -1;
}
}
// Consume data
available = false;
System.out.println("Consumer consumed: " + data);
// Notify producer
notify();
return data;
}
}
// Producer class
class Producer implements Runnable {
private Buffer buffer;
public Producer(Buffer buffer) {
this.buffer = buffer;
}
@Override
public void run() {
try {
for (int i = 1; !Thread.currentThread().isInterrupted(); i++) {
buffer.put(i);
Thread.sleep(1000); // Produce every 1 second
}
} catch (InterruptedException e) {
// Thread interrupted, exit gracefully
System.out.println("Producer thread interrupted.");
}
}
}
// Consumer class
class Consumer implements Runnable {
private Buffer buffer;
public Consumer(Buffer buffer) {
this.buffer = buffer;
}
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
buffer.get();
Thread.sleep(1500); // Consume every 1.5 seconds
}
} catch (InterruptedException e) {
// Thread interrupted, exit gracefully
System.out.println("Consumer thread interrupted.");
}
}
}