用java实现图形化进程调度代码RR
时间: 2023-09-30 08:09:46 浏览: 87
进程调度的源代码(java语言)
3星 · 编辑精心推荐
以下是使用Java实现轮转调度算法(Round Robin)的图形化进程调度代码:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ProcessScheduler extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private static final int WINDOW_WIDTH = 800;
private static final int WINDOW_HEIGHT = 600;
private static final int PROCESS_WIDTH = 20;
private static final int PROCESS_HEIGHT = 20;
private static final int PROCESS_GAP = 10;
private static final int MAX_QUANTUM = 5;
private static final int TIMER_DELAY = 100;
private static final Color[] PROCESS_COLORS = { Color.RED, Color.YELLOW,
Color.GREEN, Color.ORANGE, Color.BLUE, Color.CYAN, Color.MAGENTA };
private ArrayList<Process> processes;
private Timer timer;
private int quantum;
private int currentProcessIndex;
private int[] processQuantums;
private int[] processWaitingTimes;
private JButton startButton;
private JButton resetButton;
public ProcessScheduler() {
processes = new ArrayList<Process>();
Random random = new Random();
for (int i = 0; i < PROCESS_COLORS.length; i++) {
int x = PROCESS_GAP + (PROCESS_WIDTH + PROCESS_GAP) * i;
int y = PROCESS_GAP;
int quantum = random.nextInt(MAX_QUANTUM) + 1;
processes.add(new Process(PROCESS_COLORS[i], x, y, quantum));
}
quantum = MAX_QUANTUM;
currentProcessIndex = 0;
processQuantums = new int[processes.size()];
processWaitingTimes = new int[processes.size()];
for (int i = 0; i < processQuantums.length; i++) {
processQuantums[i] = processes.get(i).getQuantum();
}
setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
setBackground(Color.WHITE);
startButton = new JButton("Start");
startButton.addActionListener(this);
resetButton = new JButton("Reset");
resetButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(startButton);
buttonPanel.add(resetButton);
JFrame frame = new JFrame("Round Robin Process Scheduler");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this, BorderLayout.CENTER);
frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
timer = new Timer(TIMER_DELAY, this);
}
public void start() {
timer.start();
}
public void reset() {
timer.stop();
for (Process process : processes) {
process.setX(PROCESS_GAP + (PROCESS_WIDTH + PROCESS_GAP)
* processes.indexOf(process));
process.setY(PROCESS_GAP);
}
quantum = MAX_QUANTUM;
currentProcessIndex = 0;
for (int i = 0; i < processQuantums.length; i++) {
processQuantums[i] = processes.get(i).getQuantum();
processWaitingTimes[i] = 0;
}
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Process process : processes) {
process.paint(g);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
start();
} else if (e.getSource() == resetButton) {
reset();
} else if (e.getSource() == timer) {
if (processQuantums[currentProcessIndex] > 0) {
processQuantums[currentProcessIndex]--;
for (int i = 0; i < processQuantums.length; i++) {
if (i != currentProcessIndex && processQuantums[i] > 0) {
processWaitingTimes[i]++;
}
}
repaint();
} else {
processQuantums[currentProcessIndex] = processes.get(
currentProcessIndex).getQuantum();
processWaitingTimes[currentProcessIndex] = 0;
currentProcessIndex = (currentProcessIndex + 1)
% processes.size();
quantum = MAX_QUANTUM;
}
}
}
private class Process {
private Color color;
private int x;
private int y;
private int quantum;
public Process(Color color, int x, int y, int quantum) {
this.color = color;
this.x = x;
this.y = y;
this.quantum = quantum;
}
public Color getColor() {
return color;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getQuantum() {
return quantum;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void paint(Graphics g) {
g.setColor(color);
g.fillOval(x, y, PROCESS_WIDTH, PROCESS_HEIGHT);
g.setColor(Color.BLACK);
g.drawOval(x, y, PROCESS_WIDTH, PROCESS_HEIGHT);
g.drawString(Integer.toString(quantum), x + PROCESS_WIDTH / 2
- 5, y + PROCESS_HEIGHT / 2 + 5);
}
}
public static void main(String[] args) {
new ProcessScheduler();
}
}
```
这个程序将创建七个进程,每个进程都有不同的颜色和随机的量子(quantum)。程序将使用轮转调度算法(Round Robin)来调度这些进程,其中每个进程被分配一个量子时间(quantum),然后在等待队列中等待下一个量子时间。在每个量子时间结束时,当前进程将被挂起并将控制权移交给下一个进程。如果一个进程在一个量子时间内完成了它的任务,它将被立即挂起并将控制权移交给下一个进程。如果一个进程在它的量子时间内没有完成它的任务,它将被挂起并将被放回等待队列的末尾。程序将显示每个进程的颜色、量子时间和等待时间,并将在每个量子时间结束时更新等待时间。程序还提供了一个“开始”按钮和一个“重置”按钮,用户可以使用它们来控制程序的运行。
阅读全文