时间片轮转算法javaGUI实现代码,有创建进程按钮,包括开始时间和运行时间添加框,表格动态显示运行过程及状态,并设置按钮显示最终调度记录
时间: 2023-08-31 14:18:43 浏览: 101
进程调度 时间片轮转算法 操作系统
4星 · 用户满意度95%
### 回答1:
以下是一个简单的时间片轮转算法的Java GUI实现代码,包括创建进程按钮、开始时间和运行时间添加框、表格动态显示运行过程及状态,以及设置按钮显示最终调度记录。
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class TimeSliceScheduling extends JFrame implements ActionListener {
private JLabel processLabel, arrivalLabel, burstLabel, timeSliceLabel;
private JTextField processText, arrivalText, burstText, timeSliceText;
private JButton createButton, scheduleButton, resultButton;
private JTable processTable;
private DefaultTableModel model;
private ArrayList<Process> processList;
private int currentTime;
public TimeSliceScheduling() {
this.setTitle("时间片轮转算法");
this.setSize(600, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
// 创建进程输入框和按钮
JPanel inputPanel = new JPanel(new GridLayout(4, 2));
processLabel = new JLabel("进程名");
arrivalLabel = new JLabel("到达时间");
burstLabel = new JLabel("运行时间");
timeSliceLabel = new JLabel("时间片");
processText = new JTextField();
arrivalText = new JTextField();
burstText = new JTextField();
timeSliceText = new JTextField();
createButton = new JButton("创建进程");
createButton.addActionListener(this);
inputPanel.add(processLabel);
inputPanel.add(processText);
inputPanel.add(arrivalLabel);
inputPanel.add(arrivalText);
inputPanel.add(burstLabel);
inputPanel.add(burstText);
inputPanel.add(timeSliceLabel);
inputPanel.add(timeSliceText);
inputPanel.add(createButton);
this.add(inputPanel, BorderLayout.NORTH);
// 进程表格
model = new DefaultTableModel(new Object[][]{}, new String[]{"进程名", "到达时间", "运行时间", "状态"});
processTable = new JTable(model);
JScrollPane scrollPane = new JScrollPane(processTable);
this.add(scrollPane, BorderLayout.CENTER);
// 调度按钮
scheduleButton = new JButton("开始调度");
scheduleButton.addActionListener(this);
resultButton = new JButton("显示调度记录");
resultButton.addActionListener(this);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(scheduleButton);
buttonPanel.add(resultButton);
this.add(buttonPanel, BorderLayout.SOUTH);
this.setVisible(true);
}
public static void main(String[] args) {
new TimeSliceScheduling();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == createButton) {
// 创建进程
String processName = processText.getText();
int arrivalTime = Integer.parseInt(arrivalText.getText());
int burstTime = Integer.parseInt(burstText.getText());
int timeSlice = Integer.parseInt(timeSliceText.getText());
Process process = new Process(processName, arrivalTime, burstTime, timeSlice);
model.addRow(new String[]{processName, Integer.toString(arrivalTime), Integer.toString(burstTime), "未运行"});
processList.add(process);
} else if (e.getSource() == scheduleButton) {
// 开始调度
currentTime = 0;
while (!isAllFinished()) {
for (Process process : processList) {
if (process.getStatus().equals("未运行") && process.getArrivalTime() <= currentTime) {
process.setStatus("运行中");
model.setValueAt("运行中", processList.indexOf(process), 3);
} else if (process.getStatus().equals("运行中")) {
if (process.getRemainingTime() > process.getTimeSlice()) {
process.reduceRemainingTime(process.getTimeSlice());
currentTime += process.getTimeSlice();
} else {
currentTime += process.getRemainingTime();
process.reduceRemainingTime(process.getRemainingTime());
process.setStatus("已完成");
model.setValueAt("已完成", processList.indexOf(process), 3);
}
}
}
}
} else if (e.getSource() == resultButton) {
// 显示调度记录
StringBuilder result = new StringBuilder();
for (Process process : processList) {
result.append(process.getName()).append(": ").append(process.getStartTime()).append("-").append(process.getEndTime()).append("\n");
}
JOptionPane.showMessageDialog(null, result.toString(), "调度记录", JOptionPane.PLAIN_MESSAGE);
}
}
private boolean isAllFinished() {
for (Process process : processList) {
if (!process.getStatus().equals("已完成")) {
return false;
}
}
return true;
}
class Process {
private String name;
private int arrivalTime;
private int burstTime;
private int timeSlice;
private int remainingTime;
private int startTime;
private int endTime;
private String status;
public Process(String name, int arrivalTime, int burstTime, int timeSlice) {
this.name = name;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.timeSlice = timeSlice;
this.remainingTime = burstTime;
this.status = "未运行";
}
public String getName() {
return name;
}
public int getArrivalTime() {
return arrivalTime;
}
public int getBurstTime() {
return burstTime;
}
public int getTimeSlice() {
return timeSlice;
}
public int getRemainingTime() {
return remainingTime;
}
public void reduceRemainingTime(int time) {
this.remainingTime -= time;
if (this.remainingTime == 0) {
this.endTime = currentTime;
}
}
public int getStartTime() {
return startTime;
}
public void setStartTime(int startTime) {
this.startTime = startTime;
}
public int getEndTime() {
return endTime;
}
public void setEndTime(int endTime) {
this.endTime = endTime;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
if (status.equals("运行中")) {
if (this.startTime == 0) {
this.startTime = currentTime;
}
}
}
}
}
```
### 回答2:
以下是一个使用Java GUI实现时间片轮转算法的简单示例代码:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
public class TimeSliceSchedulerGUI extends JFrame {
private JButton createProcessButton;
private JButton showScheduleButton;
private JLabel startTimeLabel;
private JLabel runTimeLabel;
private JTextField startTimeField;
private JTextField runTimeField;
private JTable scheduleTable;
private DefaultTableModel tableModel;
private List<Process> processes;
private List<Process> schedule;
public TimeSliceSchedulerGUI() {
// 初始化窗口和组件
this.setTitle("时间片轮转算法演示");
this.setSize(400, 300);
createProcessButton = new JButton("创建进程");
showScheduleButton = new JButton("显示调度记录");
startTimeLabel = new JLabel("开始时间:");
runTimeLabel = new JLabel("运行时间:");
startTimeField = new JTextField(10);
runTimeField = new JTextField(10);
Object[] columnNames = {"进程号", "开始时间", "运行时间", "状态"};
Object[][] rowData = {};
tableModel = new DefaultTableModel(rowData, columnNames);
scheduleTable = new JTable(tableModel);
processes = new ArrayList<>();
schedule = new ArrayList<>();
createProcessButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int startTime = Integer.parseInt(startTimeField.getText());
int runTime = Integer.parseInt(runTimeField.getText());
Process process = new Process(startTime, runTime);
processes.add(process);
Object[] row = {processes.size(), startTime, runTime, "等待"};
tableModel.addRow(row);
startTimeField.setText("");
runTimeField.setText("");
}
});
showScheduleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
schedule = TimeSliceScheduler.schedule(processes);
for (Process process : schedule) {
int index = process.getId() - 1;
tableModel.setValueAt("运行", index, 3);
}
}
});
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2, 5, 5));
panel.add(startTimeLabel);
panel.add(startTimeField);
panel.add(runTimeLabel);
panel.add(runTimeField);
panel.add(createProcessButton);
panel.add(showScheduleButton);
this.setLayout(new BorderLayout());
this.add(panel, BorderLayout.NORTH);
this.add(new JScrollPane(scheduleTable), BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new TimeSliceSchedulerGUI();
}
}
```
```java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class TimeSliceScheduler {
public static List<Process> schedule(List<Process> processes) {
List<Process> schedule = new ArrayList<>();
int timeSlice = 2;
Queue<Process> queue = new LinkedList<>();
int currentTime = 0;
while (!processes.isEmpty() || !queue.isEmpty()) {
while (!processes.isEmpty() && processes.get(0).getStartTime() <= currentTime) {
queue.add(processes.remove(0));
}
if (!queue.isEmpty()) {
Process currentProcess = queue.remove();
schedule.add(currentProcess);
if (currentProcess.getRemainingTime() <= timeSlice) {
currentTime += currentProcess.getRemainingTime();
currentProcess.setRemainingTime(0);
} else {
currentTime += timeSlice;
currentProcess.setRemainingTime(currentProcess.getRemainingTime() - timeSlice);
queue.add(currentProcess);
}
} else {
currentTime++;
}
}
return schedule;
}
}
```
上述代码实现了一个简单的时间片轮转算法的Java GUI演示程序。程序包括了创建进程按钮,可以通过输入开始时间和运行时间创建进程,并将进程信息以表格形式动态显示出来。点击显示调度记录按钮后,程序会使用时间片轮转算法对创建的进程进行调度,并将调度记录显示在表格中。进程的状态会根据运行过程进行实时更新,最终调度记录可通过按钮显示。
注意:以上代码仅为示例,仅包含基本功能,实际应用中可能需要根据需要进行适当修改和完善。
阅读全文