通过javaGUI实现进程调度的时间片轮转法具体代码
时间: 2023-09-23 19:04:25 浏览: 89
以下是一个简单的Java GUI实现进程调度时间片轮转法的示例代码:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ProcessScheduler extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField pidField, burstTimeField;
private JTextArea outputArea;
private JButton addProcessBtn, startBtn;
private Timer timer;
private int quantum = 10; // 时间片长度
private int time = 0; // 当前时间
private int currentPid = -1; // 当前正在执行的进程的PID
private ProcessQueue processQueue = new ProcessQueue();
public ProcessScheduler() {
super("进程调度器");
// 设置窗口大小
setSize(500, 500);
// 创建GUI组件
pidField = new JTextField(5);
burstTimeField = new JTextField(5);
outputArea = new JTextArea(20, 40);
addProcessBtn = new JButton("添加进程");
startBtn = new JButton("开始");
// 添加GUI组件
JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("进程ID:"));
inputPanel.add(pidField);
inputPanel.add(new JLabel("运行时间:"));
inputPanel.add(burstTimeField);
inputPanel.add(addProcessBtn);
JPanel controlPanel = new JPanel();
controlPanel.add(startBtn);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(new JScrollPane(outputArea), BorderLayout.CENTER);
contentPane.add(controlPanel, BorderLayout.SOUTH);
// 绑定事件处理器
addProcessBtn.addActionListener(this);
startBtn.addActionListener(this);
// 创建定时器
timer = new Timer(100, this);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == addProcessBtn) {
// 添加新进程
int pid = Integer.parseInt(pidField.getText());
int burstTime = Integer.parseInt(burstTimeField.getText());
Process process = new Process(pid, burstTime);
processQueue.add(process);
outputArea.append("添加进程 " + pid + ",运行时间 " + burstTime + "\n");
} else if (source == startBtn) {
// 开始调度
startBtn.setEnabled(false);
timer.start();
} else if (source == timer) {
// 执行调度
Process currentProcess = processQueue.getCurrentProcess();
if (currentProcess != null) {
if (currentProcess.getBurstTime() > quantum) {
// 执行时间片长度的操作
currentProcess.run(quantum);
time += quantum;
outputArea.append("时间 " + time + ":进程 " + currentProcess.getPid() + " 执行了 " + quantum + " ms\n");
} else {
// 执行剩下的操作
currentProcess.run(currentProcess.getBurstTime());
time += currentProcess.getBurstTime();
outputArea.append("时间 " + time + ":进程 " + currentProcess.getPid() + " 执行了 " + currentProcess.getBurstTime() + " ms\n");
processQueue.removeCurrentProcess();
}
} else {
// 所有进程执行完毕
timer.stop();
startBtn.setEnabled(true);
}
}
}
public static void main(String[] args) {
ProcessScheduler scheduler = new ProcessScheduler();
scheduler.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scheduler.setVisible(true);
}
}
class Process {
private int pid;
private int burstTime;
public Process(int pid, int burstTime) {
this.pid = pid;
this.burstTime = burstTime;
}
public int getPid() {
return pid;
}
public int getBurstTime() {
return burstTime;
}
public void run(int quantum) {
burstTime -= quantum;
}
}
class ProcessQueue {
private java.util.Queue<Process> queue = new java.util.LinkedList<Process>();
public void add(Process process) {
queue.add(process);
}
public Process getCurrentProcess() {
if (queue.isEmpty()) {
return null;
} else {
Process currentProcess = queue.peek();
if (currentProcess.getBurstTime() <= 0) {
queue.remove();
return getCurrentProcess();
} else {
return currentProcess;
}
}
}
public void removeCurrentProcess() {
queue.remove();
}
}
```
以上代码仅供参考,具体实现还需根据实际需求进行调整和优化。
阅读全文