生产者消费者模型时间片轮转算法java窗口实现完整代码
时间: 2023-11-12 20:00:14 浏览: 149
Java语言时间片轮转算法
4星 · 用户满意度95%
以下是一个基于Java和Swing的生产者消费者模型时间片轮转算法的窗口实现完整代码:
```
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ProducerConsumer extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 600;
private static final int HEIGHT = 400;
private static final int BUFFER_SIZE = 10;
private static final int PRODUCER_DELAY = 1000;
private static final int CONSUMER_DELAY = 2000;
private static final int TIME_QUANTUM = 100;
private static final int MAX_PRODUCE = 3;
private static final int MAX_CONSUME = 2;
private static final int PRODUCE_BUTTON_INDEX = 0;
private static final int CONSUME_BUTTON_INDEX = 1;
private static final String PRODUCE_BUTTON_TEXT = "Start Producer";
private static final String CONSUME_BUTTON_TEXT = "Start Consumer";
private static final String PRODUCE_STATUS_TEXT = "Producer Status: %s";
private static final String CONSUME_STATUS_TEXT = "Consumer Status: %s";
private static final String BUFFER_STATUS_TEXT = "Buffer Status: %d/%d";
private static final String LOG_FORMAT = "[%s] %s %d\n";
private static final String EMPTY_BUFFER_INDICATOR = "-";
private final Object lock = new Object();
private final Random random = new Random();
private final Queue<Integer> buffer = new LinkedList<>();
private final Timer producerTimer = new Timer(PRODUCER_DELAY, this);
private final Timer consumerTimer = new Timer(CONSUMER_DELAY, this);
private final JTextArea logArea = new JTextArea();
private final JLabel produceStatusLabel = new JLabel(String.format(PRODUCE_STATUS_TEXT, "Stopped"));
private final JLabel consumeStatusLabel = new JLabel(String.format(CONSUME_STATUS_TEXT, "Stopped"));
private final JLabel bufferStatusLabel = new JLabel(String.format(BUFFER_STATUS_TEXT, 0, BUFFER_SIZE));
private final JButton[] buttons = { new JButton(PRODUCE_BUTTON_TEXT), new JButton(CONSUME_BUTTON_TEXT) };
private boolean producerRunning = false;
private boolean consumerRunning = false;
private int timeLeft = TIME_QUANTUM;
public ProducerConsumer() {
super("Producer Consumer Simulation");
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel controlPanel = new JPanel(new FlowLayout());
for (JButton button : buttons) {
button.addActionListener(this);
controlPanel.add(button);
}
JPanel statusPanel = new JPanel(new FlowLayout());
statusPanel.add(produceStatusLabel);
statusPanel.add(consumeStatusLabel);
statusPanel.add(bufferStatusLabel);
JPanel logPanel = new JPanel(new BorderLayout());
logPanel.add(new JLabel("Log:"), BorderLayout.NORTH);
logPanel.add(logArea, BorderLayout.CENTER);
add(controlPanel, BorderLayout.NORTH);
add(statusPanel, BorderLayout.CENTER);
add(logPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void produce() {
int count = random.nextInt(MAX_PRODUCE) + 1;
synchronized (lock) {
while (buffer.size() + count > BUFFER_SIZE) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < count; i++) {
buffer.add(random.nextInt(100));
}
bufferStatusLabel.setText(String.format(BUFFER_STATUS_TEXT, buffer.size(), BUFFER_SIZE));
logArea.append(String.format(LOG_FORMAT, "PRODUCE", "Produced", count));
lock.notifyAll();
}
}
private void consume() {
int count = random.nextInt(MAX_CONSUME) + 1;
synchronized (lock) {
while (buffer.size() < count) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < count; i++) {
buffer.poll();
}
bufferStatusLabel.setText(String.format(BUFFER_STATUS_TEXT, buffer.size(), BUFFER_SIZE));
logArea.append(String.format(LOG_FORMAT, "CONSUME", "Consumed", count));
lock.notifyAll();
}
}
private void runProducer() {
producerRunning = true;
produceStatusLabel.setText(String.format(PRODUCE_STATUS_TEXT, "Running"));
producerTimer.start();
}
private void runConsumer() {
consumerRunning = true;
consumeStatusLabel.setText(String.format(CONSUME_STATUS_TEXT, "Running"));
consumerTimer.start();
}
private void stopProducer() {
producerRunning = false;
produceStatusLabel.setText(String.format(PRODUCE_STATUS_TEXT, "Stopped"));
producerTimer.stop();
}
private void stopConsumer() {
consumerRunning = false;
consumeStatusLabel.setText(String.format(CONSUME_STATUS_TEXT, "Stopped"));
consumerTimer.stop();
}
private void logEmptyBuffer() {
logArea.append(String.format(LOG_FORMAT, "CONSUME", "Buffer is empty", 0));
}
private void runTimeSlice() {
timeLeft -= TIME_QUANTUM;
if (timeLeft <= 0) {
timeLeft = TIME_QUANTUM;
synchronized (lock) {
lock.notifyAll();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttons[PRODUCE_BUTTON_INDEX]) {
if (producerRunning) {
stopProducer();
} else {
runProducer();
}
} else if (e.getSource() == buttons[CONSUME_BUTTON_INDEX]) {
if (consumerRunning) {
stopConsumer();
} else {
runConsumer();
}
} else if (e.getSource() == producerTimer) {
produce();
} else if (e.getSource() == consumerTimer) {
synchronized (lock) {
if (buffer.isEmpty()) {
logEmptyBuffer();
lock.notifyAll();
} else {
consume();
}
}
}
runTimeSlice();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ProducerConsumer();
}
});
}
}
```
在这个实现中,我们使用了两个Timer对象来模拟生产者和消费者的生产和消费行为。每当Timer对象触发ActionEvent时,我们会调用相应的生产或消费方法。
对于生产方法,我们随机生成一定数量的数字,并把它们添加到缓冲区中。如果缓冲区已满,我们会等待直到有足够的空间来添加新的数字。当生产完成后,我们会更新缓冲区状态并记录日志,然后唤醒任何等待的线程。
对于消费方法,我们随机生成一定数量的数字,并从缓冲区中移除它们。如果缓冲区为空,我们会记录日志并唤醒任何等待的线程。当消费完成后,我们会更新缓冲区状态并记录日志,然后唤醒任何等待的线程。
我们还实现了时间片轮转算法,并在每个时间片结束时唤醒任何等待的线程。这是为了确保在生产者和消费者之间公平地分配CPU时间。
最后,我们使用Swing创建了一个简单的GUI界面,使用户能够启动和停止生产者和消费者。我们还在界面上显示了生产者和消费者的状态以及缓冲区的状态和日志。
阅读全文