基于Java Swing实现时间片轮转算法的演示程序,通过JTable动态输出显示运行过程,并有创建进程的jbutton
时间: 2023-06-14 09:05:07 浏览: 139
程序实现基于优先数的时间片轮转调度算法调度处理器
5星 · 资源好评率100%
以下是基于Java Swing实现时间片轮转算法的演示程序的代码。该程序通过JTable动态输出显示运行过程,并提供了创建进程的JButton。
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TimeSliceRoundRobin extends JFrame implements ActionListener {
private int numOfProcesses; // 进程数量
private int timeQuantum; // 时间片大小
private int[] arrivalTime; // 到达时间
private int[] burstTime; // 执行时间
private int[] remainingTime; // 剩余执行时间
private int[] waitingTime; // 等待时间
private int[] turnaroundTime; // 周转时间
private int[] completionTime; // 完成时间
private JPanel inputPanel; // 输入面板
private JLabel lblNumOfProcesses; // 进程数量标签
private JTextField txtNumOfProcesses; // 进程数量输入框
private JLabel lblTimeQuantum; // 时间片大小标签
private JTextField txtTimeQuantum; // 时间片大小输入框
private JButton btnCreateProcesses; // 创建进程按钮
private JPanel outputPanel; // 输出面板
private JTable tblProcesses; // 进程表格
private DefaultTableModel tblModel; // 表格模型
private JButton btnRun; // 运行按钮
public TimeSliceRoundRobin() {
setTitle("时间片轮转算法演示程序");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 初始化输入面板
inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(3, 2));
lblNumOfProcesses = new JLabel("进程数量:");
inputPanel.add(lblNumOfProcesses);
txtNumOfProcesses = new JTextField();
inputPanel.add(txtNumOfProcesses);
lblTimeQuantum = new JLabel("时间片大小:");
inputPanel.add(lblTimeQuantum);
txtTimeQuantum = new JTextField();
inputPanel.add(txtTimeQuantum);
btnCreateProcesses = new JButton("创建进程");
btnCreateProcesses.addActionListener(this);
inputPanel.add(btnCreateProcesses);
// 初始化输出面板
outputPanel = new JPanel();
outputPanel.setLayout(new BorderLayout());
tblModel = new DefaultTableModel();
tblProcesses = new JTable(tblModel);
JScrollPane scrollPane = new JScrollPane(tblProcesses);
outputPanel.add(scrollPane, BorderLayout.CENTER);
btnRun = new JButton("运行");
btnRun.addActionListener(this);
outputPanel.add(btnRun, BorderLayout.SOUTH);
// 添加面板
add(inputPanel, BorderLayout.NORTH);
add(outputPanel, BorderLayout.CENTER);
}
// 创建进程
private void createProcesses() {
numOfProcesses = Integer.parseInt(txtNumOfProcesses.getText());
timeQuantum = Integer.parseInt(txtTimeQuantum.getText());
// 初始化数组
arrivalTime = new int[numOfProcesses];
burstTime = new int[numOfProcesses];
remainingTime = new int[numOfProcesses];
waitingTime = new int[numOfProcesses];
turnaroundTime = new int[numOfProcesses];
completionTime = new int[numOfProcesses];
// 随机生成到达时间和执行时间
for (int i = 0; i < numOfProcesses; i++) {
arrivalTime[i] = (int) (Math.random() * 10);
burstTime[i] = (int) (Math.random() * 10 + 1);
remainingTime[i] = burstTime[i];
}
// 更新表格模型
tblModel.setColumnCount(numOfProcesses + 1);
tblModel.setRowCount(6);
tblModel.setValueAt("进程", 0, 0);
tblModel.setValueAt("到达时间", 1, 0);
tblModel.setValueAt("执行时间", 2, 0);
tblModel.setValueAt("剩余执行时间", 3, 0);
tblModel.setValueAt("等待时间", 4, 0);
tblModel.setValueAt("周转时间", 5, 0);
for (int i = 0; i < numOfProcesses; i++) {
tblModel.setValueAt("P" + (i + 1), 0, i + 1);
tblModel.setValueAt(arrivalTime[i], 1, i + 1);
tblModel.setValueAt(burstTime[i], 2, i + 1);
tblModel.setValueAt(remainingTime[i], 3, i + 1);
}
}
// 运行算法
private void runAlgorithm() {
int time = 0; // 当前时间
int totalWaitingTime = 0; // 总等待时间
int totalTurnaroundTime = 0; // 总周转时间
while (true) {
boolean isCompleted = true;
for (int i = 0; i < numOfProcesses; i++) {
if (remainingTime[i] > 0) {
isCompleted = false;
if (remainingTime[i] > timeQuantum) {
time += timeQuantum;
remainingTime[i] -= timeQuantum;
} else {
time += remainingTime[i];
completionTime[i] = time;
remainingTime[i] = 0;
waitingTime[i] = completionTime[i] - arrivalTime[i] - burstTime[i];
turnaroundTime[i] = completionTime[i] - arrivalTime[i];
totalWaitingTime += waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
}
}
}
if (isCompleted) {
break;
}
}
// 更新表格模型
for (int i = 0; i < numOfProcesses; i++) {
tblModel.setValueAt(completionTime[i], 1, i + 1);
tblModel.setValueAt(waitingTime[i], 4, i + 1);
tblModel.setValueAt(turnaroundTime[i], 5, i + 1);
}
// 显示平均等待时间和平均周转时间
JOptionPane.showMessageDialog(this, "平均等待时间:" + (double) totalWaitingTime / numOfProcesses
+ "\n平均周转时间:" + (double) totalTurnaroundTime / numOfProcesses);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnCreateProcesses) {
createProcesses();
} else if (e.getSource() == btnRun) {
runAlgorithm();
}
}
public static void main(String[] args) {
TimeSliceRoundRobin frame = new TimeSliceRoundRobin();
frame.setVisible(true);
}
}
```
程序中使用了两个面板,一个用于输入参数,一个用于输出结果。在输入面板中,用户输入进程数量和时间片大小,并点击“创建进程”按钮随机生成到达时间和执行时间。在输出面板中,使用JTable动态输出当前进程的状态,包括到达时间、执行时间、剩余执行时间、等待时间、周转时间和完成时间。当用户点击“运行”按钮时,程序会运行时间片轮转算法,并计算平均等待时间和平均周转时间并显示。
阅读全文