-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuiSemaphors.java
99 lines (83 loc) · 3.38 KB
/
GuiSemaphors.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
89
90
91
92
93
94
95
96
97
98
99
import javax.swing.*;
import java.awt.*;
import java.util.Queue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
public class GuiSemaphores {
private JFrame frame;
private JTextArea textArea;
private Queue<Integer> buffer;
private Semaphore bufferLock;
private Semaphore items;
private Semaphore spaces;
private AtomicInteger producerCount;
private AtomicInteger consumerCount;
private Runnable producerTask;
private Runnable consumerTask;
public GuiSemaphores(Queue<Integer> initialBuffer, Semaphore bufferLock, Semaphore items, Semaphore spaces, AtomicInteger producerCount, AtomicInteger consumerCount) {
this.buffer = initialBuffer;
this.bufferLock = bufferLock;
this.items = items;
this.spaces = spaces;
this.producerCount = producerCount;
this.consumerCount = consumerCount;
initializeUI();
}
private void initializeUI() {
frame = new JFrame("Producer Consumer Simulation");
frame.setSize(600, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea(15, 30);
textArea.setEditable(false);
JPanel panel = new JPanel();
JButton addProducer = new JButton("Add Producer");
JButton removeProducer = new JButton("Remove Producer");
JButton addConsumer = new JButton("Add Consumer");
JButton removeConsumer = new JButton("Remove Consumer");
addProducer.addActionListener(e -> {
producerCount.incrementAndGet();
new Thread(producerTask).start();
log("Added producer. Total producers: " + producerCount.get());
});
removeProducer.addActionListener(e -> {
if (producerCount.get() > 0) {
producerCount.decrementAndGet();
log("Removed producer. Total producers: " + producerCount.get());
}
});
addConsumer.addActionListener(e -> {
consumerCount.incrementAndGet();
new Thread(consumerTask).start();
log("Added consumer. Total consumers: " + consumerCount.get());
});
removeConsumer.addActionListener(e -> {
if (consumerCount.get() > 0) {
consumerCount.decrementAndGet();
log("Removed consumer. Total consumers: " + consumerCount.get());
}
});
panel.add(addProducer);
panel.add(removeProducer);
panel.add(addConsumer);
panel.add(removeConsumer);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(textArea), BorderLayout.CENTER);
frame.add(panel, BorderLayout.SOUTH);
frame.setVisible(true);
}
public void log(String message) {
SwingUtilities.invokeLater(() -> textArea.append(message + "\n"));
}
public void logBufferFull() {
SwingUtilities.invokeLater(() -> textArea.append("Buffer is full\n"));
}
public void logBufferEmpty() {
SwingUtilities.invokeLater(() -> textArea.append("Buffer is empty\n"));
}
public void setProducerTask(Runnable producerTask) {
this.producerTask = producerTask;
}
public void setConsumerTask(Runnable consumerTask) {
this.consumerTask = consumerTask;
}
}