通过javaGUI实现进程调度的优先级调度算法具体代码
时间: 2024-03-23 11:39:40 浏览: 117
以下是一个简单的基于Java GUI实现的优先级调度算法的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class PriorityScheduling extends JFrame implements ActionListener {
private JLabel processLabel, burstLabel, priorityLabel;
private JTextField processField, burstField, priorityField;
private JButton addButton, simulateButton;
private JTextArea resultArea;
private ArrayList<Process> processList;
private JPanel inputPanel, buttonPanel;
private JScrollPane scrollPane;
public PriorityScheduling() {
setTitle("Priority Scheduling");
setSize(600, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
processLabel = new JLabel("Process");
burstLabel = new JLabel("Burst Time");
priorityLabel = new JLabel("Priority");
processField = new JTextField(10);
burstField = new JTextField(10);
priorityField = new JTextField(10);
addButton = new JButton("Add");
addButton.addActionListener(this);
simulateButton = new JButton("Simulate");
simulateButton.addActionListener(this);
resultArea = new JTextArea(15, 40);
resultArea.setEditable(false);
inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(processLabel);
inputPanel.add(processField);
inputPanel.add(burstLabel);
inputPanel.add(burstField);
inputPanel.add(priorityLabel);
inputPanel.add(priorityField);
buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(simulateButton);
scrollPane = new JScrollPane(resultArea);
setLayout(new BorderLayout());
add(inputPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
add(scrollPane, BorderLayout.SOUTH);
processList = new ArrayList<Process>();
}
public static void main(String[] args) {
PriorityScheduling frame = new PriorityScheduling();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String processName = processField.getText();
int burstTime = Integer.parseInt(burstField.getText());
int priority = Integer.parseInt(priorityField.getText());
Process process = new Process(processName, burstTime, priority);
processList.add(process);
processField.setText("");
burstField.setText("");
priorityField.setText("");
} else if (e.getSource() == simulateButton) {
simulate();
}
}
private void simulate() {
// Sort the process list based on priority
Collections.sort(processList, new Comparator<Process>() {
public int compare(Process p1, Process p2) {
return p1.getPriority() - p2.getPriority();
}
});
int currentTime = 0;
int totalWaitingTime = 0;
int totalTurnaroundTime = 0;
resultArea.setText("");
resultArea.append("Process\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
for (Process process : processList) {
int waitingTime = currentTime;
int turnaroundTime = currentTime + process.getBurstTime();
totalWaitingTime += waitingTime;
totalTurnaroundTime += turnaroundTime;
resultArea.append(process.getProcessName() + "\t" + process.getBurstTime() + "\t\t" + process.getPriority() + "\t\t" + waitingTime + "\t\t" + turnaroundTime + "\n");
currentTime += process.getBurstTime();
}
double averageWaitingTime = (double) totalWaitingTime / processList.size();
double averageTurnaroundTime = (double) totalTurnaroundTime / processList.size();
resultArea.append("\nAverage Waiting Time: " + averageWaitingTime + "\n");
resultArea.append("Average Turnaround Time: " + averageTurnaroundTime + "\n");
}
private class Process {
private String processName;
private int burstTime;
private int priority;
public Process(String processName, int burstTime, int priority) {
this.processName = processName;
this.burstTime = burstTime;
this.priority = priority;
}
public String getProcessName() {
return processName;
}
public int getBurstTime() {
return burstTime;
}
public int getPriority() {
return priority;
}
}
}
```
该程序包括一个窗口,用户可以添加进程并模拟优先级调度算法。用户可以在窗口中输入进程名称,进程的执行时间和优先级,并单击“添加”按钮将进程添加到调度队列中。当用户单击“模拟”按钮时,程序将使用优先级调度算法模拟进程执行,并在窗口中显示每个进程的等待时间和周转时间,以及平均等待时间和平均周转时间。
阅读全文