Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProducerConsumer.java
More file actions
Latest commit
123 lines (103 loc) · 3.24 KB
/
Copy pathProducerConsumer.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
publicclassProducerConsumer {
publicstaticvoidmain(String[] args) {
System.out.println("Producer Consumer Problem");
System.out.println("-----------------------");
// Create shared buffer
Bufferbuffer = newBuffer();
// Create producer and consumer threads
ThreadproducerThread = newThread(newProducer(buffer));
ThreadconsumerThread = newThread(newConsumer(buffer));
// Start threads
producerThread.start();
consumerThread.start();
// Let the threads run for a while
try {
Thread.sleep(10000); // Run for 10 seconds
} catch (InterruptedExceptione) {
e.printStackTrace();
}
// Interrupt the threads to stop them
producerThread.interrupt();
consumerThread.interrupt();
System.out.println("Main thread exiting.");
}
}
// Shared buffer class
classBuffer {
privateintdata;
privatebooleanavailable = false;
// Method for producer to put data
publicsynchronizedvoidput(intvalue) {
// Wait until consumer consumes the previous value
while (available) {
try {
wait();
} catch (InterruptedExceptione) {
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
publicsynchronizedintget() {
// Wait until producer puts a value
while (!available) {
try {
wait();
} catch (InterruptedExceptione) {
Thread.currentThread().interrupt();
return -1;
}
}
// Consume data
available = false;
System.out.println("Consumer consumed: " + data);
// Notify producer
notify();
returndata;
}
}
// Producer class
classProducerimplementsRunnable {
privateBufferbuffer;
publicProducer(Bufferbuffer) {
this.buffer = buffer;
}
@Override
publicvoidrun() {
try {
for (inti = 1; !Thread.currentThread().isInterrupted(); i++) {
buffer.put(i);
Thread.sleep(1000); // Produce every 1 second
}
} catch (InterruptedExceptione) {
// Thread interrupted, exit gracefully
System.out.println("Producer thread interrupted.");
}
}
}
// Consumer class
classConsumerimplementsRunnable {
privateBufferbuffer;
publicConsumer(Bufferbuffer) {
this.buffer = buffer;
}
@Override
publicvoidrun() {
try {
while (!Thread.currentThread().isInterrupted()) {
buffer.get();
Thread.sleep(1500); // Consume every 1.5 seconds
}
} catch (InterruptedExceptione) {
// Thread interrupted, exit gracefully
System.out.println("Consumer thread interrupted.");
}
}
}