时间片轮转算法javaGUI实现代码,有创建进程按钮,包括开始时间和运行时间添加框,表格动态显示运行过程及状态,并设置按钮显示最终调度记录
时间: 2023-09-09 12:04:22 浏览: 104
### 回答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;
import java.util.List;
public class TimeSliceRoundRobin {
private JFrame frame;
private JPanel panel;
private JLabel processLabel, startTimeLabel, runTimeLabel, quantumLabel;
private JTextField processField, startTimeField, runTimeField, quantumField;
private JButton createButton, startButton, recordButton;
private JTable table;
private DefaultTableModel model;
private List<Process> processList;
private int quantum;
private int currentTime;
private int finishedCount;
public static void main(String[] args) {
TimeSliceRoundRobin scheduler = new TimeSliceRoundRobin();
scheduler.init();
}
private void init() {
frame = new JFrame("Time Slice Round Robin");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new GridLayout(5, 2));
processLabel = new JLabel("Process");
panel.add(processLabel);
processField = new JTextField();
panel.add(processField);
startTimeLabel = new JLabel("Start Time");
panel.add(startTimeLabel);
startTimeField = new JTextField();
panel.add(startTimeField);
runTimeLabel = new JLabel("Run Time");
panel.add(runTimeLabel);
runTimeField = new JTextField();
panel.add(runTimeField);
quantumLabel = new JLabel("Time Quantum");
panel.add(quantumLabel);
quantumField = new JTextField();
panel.add(quantumField);
createButton = new JButton("Create");
createButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createProcess();
}
});
panel.add(createButton);
startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startScheduler();
}
});
panel.add(startButton);
recordButton = new JButton("Show Records");
recordButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showRecords();
}
});
recordButton.setEnabled(false);
panel.add(recordButton);
model = new DefaultTableModel();
model.addColumn("Process");
model.addColumn("Start Time");
model.addColumn("Run Time");
model.addColumn("Remaining Time");
model.addColumn("Status");
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
private void createProcess() {
String processName = processField.getText();
int startTime = Integer.parseInt(startTimeField.getText());
int runTime = Integer.parseInt(runTimeField.getText());
if (processList == null) {
processList = new ArrayList<>();
}
Process process = new Process(processName, startTime, runTime);
processList.add(process);
Object[] rowData = new Object[5];
rowData[0] = processName;
rowData[1] = startTime;
rowData[2] = runTime;
rowData[3] = runTime;
rowData[4] = "Waiting";
model.addRow(rowData);
processField.setText("");
startTimeField.setText("");
runTimeField.setText("");
}
private void startScheduler() {
quantum = Integer.parseInt(quantumField.getText());
startButton.setEnabled(false);
recordButton.setEnabled(true);
while (finishedCount < processList.size()) {
for (Process process : processList) {
if (process.getStatus().equals("Finished")) {
continue;
}
if (process.getStartTime() > currentTime) {
continue;
}
int remainingTime = process.getRemainingTime();
if (remainingTime <= quantum) {
currentTime += remainingTime;
process.setRemainingTime(0);
process.setStatus("Finished");
finishedCount++;
int rowIndex = getRowIndex(process.getName());
model.setValueAt(0, rowIndex, 3);
model.setValueAt("Finished", rowIndex, 4);
} else {
currentTime += quantum;
process.setRemainingTime(remainingTime - quantum);
int rowIndex = getRowIndex(process.getName());
model.setValueAt(process.getRemainingTime(), rowIndex, 3);
model.setValueAt("Running", rowIndex, 4);
}
}
}
}
private void showRecords() {
String message = "Process\tStart Time\tRun Time\tFinish Time\tTurnaround Time\n";
for (Process process : processList) {
int finishTime = process.getStartTime() + process.getRunTime();
int turnaroundTime = finishTime - process.getStartTime();
message += process.getName() + "\t" + process.getStartTime() + "\t" + process.getRunTime() + "\t" + finishTime + "\t" + turnaroundTime + "\n";
}
JOptionPane.showMessageDialog(frame, message);
}
private int getRowIndex(String processName) {
for (int i = 0; i < model.getRowCount(); i++) {
if (model.getValueAt(i, 0).equals(processName)) {
return i;
}
}
return -1;
}
private class Process {
private String name;
private int startTime;
private int runTime;
private int remainingTime;
private String status;
public Process(String name, int startTime, int runTime) {
this.name = name;
this.startTime = startTime;
this.runTime = runTime;
this.remainingTime = runTime;
this.status = "Waiting";
}
public String getName() {
return name;
}
public int getStartTime() {
return startTime;
}
public int getRunTime() {
return runTime;
}
public int getRemainingTime() {
return remainingTime;
}
public void setRemainingTime(int remainingTime) {
this.remainingTime = remainingTime;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
}
```
### 回答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;
import java.util.Timer;
import java.util.TimerTask;
public class TimeSliceRoundRobinAlgorithm extends JFrame {
private JPanel panel;
private JButton createProcessButton;
private JButton showScheduleButton;
private JTextField startTimeField;
private JTextField runTimeField;
private JTable processTable;
private DefaultTableModel tableModel;
private List<Process> processes;
private int currentProcessIndex;
public TimeSliceRoundRobinAlgorithm() {
panel = new JPanel(new BorderLayout());
createProcessButton = new JButton("创建进程");
createProcessButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createProcess();
}
});
showScheduleButton = new JButton("显示调度记录");
showScheduleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showSchedule();
}
});
JLabel startTimeLabel = new JLabel("开始时间:");
startTimeField = new JTextField();
JLabel runTimeLabel = new JLabel("运行时间:");
runTimeField = new JTextField();
JPanel inputPanel = new JPanel();
inputPanel.add(createProcessButton);
inputPanel.add(startTimeLabel);
inputPanel.add(startTimeField);
inputPanel.add(runTimeLabel);
inputPanel.add(runTimeField);
inputPanel.add(showScheduleButton);
tableModel = new DefaultTableModel(new Object[]{"进程ID", "开始时间", "运行时间", "状态"}, 0);
processTable = new JTable(tableModel);
panel.add(inputPanel, BorderLayout.NORTH);
panel.add(new JScrollPane(processTable), BorderLayout.CENTER);
processes = new ArrayList<>();
currentProcessIndex = 0;
setContentPane(panel);
setTitle("时间片轮转算法");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// 创建进程
private void createProcess() {
int startTime = Integer.parseInt(startTimeField.getText());
int runTime = Integer.parseInt(runTimeField.getText());
Process process = new Process(startTime, runTime);
processes.add(process);
tableModel.addRow(new Object[]{process.getID(), process.getStartTime(), process.getRunTime(), "就绪"});
}
// 显示调度记录
private void showSchedule() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Process currentProcess = processes.get(currentProcessIndex);
int remainingTime = currentProcess.getRemainingTime();
if (remainingTime > 0) {
if (remainingTime <= 1) {
currentProcess.setStatus("完成");
tableModel.setValueAt(currentProcess.getStatus(), currentProcessIndex, 3);
currentProcessIndex = (currentProcessIndex + 1) % processes.size();
} else {
currentProcess.decreaseRemainingTime();
currentProcessIndex = (currentProcessIndex + 1) % processes.size();
}
}
}
}, 0, 1000);
}
// 进程类
private static class Process {
private static int totalProcessCount = 0;
private int ID;
private int startTime;
private int runTime;
private int remainingTime;
private String status;
public Process(int startTime, int runTime) {
this.ID = ++totalProcessCount;
this.startTime = startTime;
this.runTime = runTime;
this.remainingTime = runTime;
this.status = "就绪";
}
public int getID() {
return ID;
}
public int getStartTime() {
return startTime;
}
public int getRunTime() {
return runTime;
}
public int getRemainingTime() {
return remainingTime;
}
public void decreaseRemainingTime() {
remainingTime--;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TimeSliceRoundRobinAlgorithm();
}
});
}
}
```
本代码实现了一个简单的时间片轮转算法的Java GUI程序。程序界面上有一个“创建进程”按钮,以及用于输入进程开始时间和运行时间的两个文本框。在程序的表格中,动态显示了进程的运行过程及状态,并设置了一个按钮用于显示最终的调度记录。当点击"创建进程"按钮时,会根据输入的开始时间和运行时间创建一个新的进程,并将该进程的信息添加到表格中。当点击"显示调度记录"按钮时,程序将按照时间片的规则,依次运行各个进程,直到所有进程都完成。在每次调度过程中,会更新表格中对应进程的状态,并在最后一个进程完成时停止调度。
### 回答3:
下面是一个简单的时间片轮转算法的Java GUI实现代码。
首先,在Java中创建一个类名为TimeSliceScheduling的类:
```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 JButton createProcessButton;
private JButton showFinalScheduleButton;
private JTextField startTimeField;
private JTextField runningTimeField;
private JTable processTable;
private DefaultTableModel tableModel;
private ArrayList<Process> processes;
private ArrayList<String[]> scheduleRecord;
private int timeSlice;
private int currentTime;
public TimeSliceScheduling() {
setTitle("时间片轮转算法");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建按钮和文本框
createProcessButton = new JButton("创建进程");
showFinalScheduleButton = new JButton("显示最终调度记录");
startTimeField = new JTextField(10);
runningTimeField = new JTextField(10);
// 创建表格
tableModel = new DefaultTableModel(new String[]{"开始时间", "运行时间"}, 0);
processTable = new JTable(tableModel);
// 添加组件到窗口
setLayout(new FlowLayout());
add(new JLabel("开始时间: "));
add(startTimeField);
add(new JLabel("运行时间: "));
add(runningTimeField);
add(createProcessButton);
add(showFinalScheduleButton);
add(new JScrollPane(processTable));
// 为按钮添加监听器
createProcessButton.addActionListener(this);
showFinalScheduleButton.addActionListener(this);
// 初始化数据结构
processes = new ArrayList<>();
scheduleRecord = new ArrayList<>();
timeSlice = 10; // 为了简化,假设时间片大小为10
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == createProcessButton) {
// 获取开始时间和运行时间
int startTime = Integer.parseInt(startTimeField.getText());
int runningTime = Integer.parseInt(runningTimeField.getText());
// 创建新进程
Process process = new Process(startTime, runningTime);
processes.add(process);
// 将进程添加到表格中
tableModel.addRow(new String[]{String.valueOf(startTime), String.valueOf(runningTime)});
} else if (e.getSource() == showFinalScheduleButton) {
// 运行调度算法
runScheduler();
// 创建最终调度记录窗口并显示记录
String[] columnNames = {"时间", "进程ID"};
String[][] rowData = new String[scheduleRecord.size()][2];
for (int i = 0; i < scheduleRecord.size(); i++) {
rowData[i] = scheduleRecord.get(i);
}
JTable finalScheduleTable = new JTable(rowData, columnNames);
JOptionPane.showMessageDialog(null, new JScrollPane(finalScheduleTable), "最终调度记录", JOptionPane.PLAIN_MESSAGE);
}
}
private void runScheduler() {
currentTime = 0;
int remainingProcesses = processes.size();
int currentProcessIndex = 0;
while (remainingProcesses > 0) {
Process currentProcess = processes.get(currentProcessIndex);
if (currentProcess.getRemainingTime() > 0) {
if (currentProcess.getRemainingTime() <= timeSlice) {
scheduleRecord.add(new String[]{String.valueOf(currentTime), String.valueOf(currentProcessIndex)});
currentTime += currentProcess.getRemainingTime();
currentProcess.setRemainingTime(0);
remainingProcesses--;
} else {
scheduleRecord.add(new String[]{String.valueOf(currentTime), String.valueOf(currentProcessIndex)});
currentTime += timeSlice;
currentProcess.setRemainingTime(currentProcess.getRemainingTime() - timeSlice);
}
}
currentProcessIndex = (currentProcessIndex + 1) % processes.size();
}
}
public static void main(String[] args) {
new TimeSliceScheduling();
}
}
class Process {
private int startTime;
private int runningTime;
private int remainingTime;
public Process(int startTime, int runningTime) {
this.startTime = startTime;
this.runningTime = runningTime;
this.remainingTime = runningTime;
}
// getter和setter方法...
public int getRemainingTime() {
return remainingTime;
}
public void setRemainingTime(int remainingTime) {
this.remainingTime = remainingTime;
}
}
```
这个程序中有两个按钮,一个用于创建进程,另一个用于显示最终的调度记录。进程的开始时间和运行时间通过文本框输入,并添加到表格中。点击显示最终调度记录按钮时,将调用`runScheduler`方法来执行时间片轮转算法,并创建一个新的窗口显示最终的调度记录。
请注意,此代码仅用于演示时间片轮转算法的基本思想和Java GUI的用法,可能还需要进行进一步的优化和完善,以适应更复杂的实际应用场景。
阅读全文