我试图运行一个带有优先级队列系统的调度程序示例。它的工作原理如下:
使用包含优先级的GUI输入新进程。 GUI命令也终止进程。上下文切换是按命令进行的,因为切换的原因并不重要。代码中使用了三个组件,包括:基于优先级的就绪队列,阻止列表以及在每个上下文切换显示就绪,阻止和运行进程后输出完整系统状态。
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.PriorityQueue;
import java.util.Queue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class dispatcher extends JFrame implements ActionListener {
static int processNo = 0;
static Process currentlyRunning;
static Queue<Process> readyQueue;
static Queue<Process> blockedQueue;
static JButton newProcess;
static JButton contextSwitch;
static JTextField priority;
static JTextArea Queues;
private static Process newProcess(int i, int i0, String running) {
throw new UnsupportedOperationException("Not supported yet.");
}
dispatcher() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
Queues = new JTextArea(5, 15);
Queues.setPreferredSize(new Dimension(500, 400));
cp.add(Queues);
cp.add(new JLabel("Priority"));
priority = new JTextField(6);
newProcess = new JButton("Create Process");
newProcess.addActionListener(this);
cp.add(priority);
cp.add(newProcess);
contextSwitch = new JButton("Context Switch");
contextSwitch.addActionListener(this);
cp.add(contextSwitch);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Dispatcher");
setSize(600, 500);
setVisible(true);
}
public static void main(String args[]) {
dispatcher d = new dispatcher();
readyQueue = new PriorityQueue<>();
blockedQueue = new PriorityQueue<>();
Process p1 = newProcess(processNo++, 2, "Running");
Process p2 = newProcess(processNo++, 1, "Blocked");
Process p3 = newProcess(processNo++, 4, "Ready");
Process p4 = newProcess(processNo++, 3, "Ready");
Process p5 = newProcess(processNo++, 5, "Ready");
Process p6 = newProcess(processNo++, 8, "Blocked");
readyQueue.add(p3);
readyQueue.add(p4);
readyQueue.add(p5);
blockedQueue.add(p6);
blockedQueue.add(p2);
currentlyRunning = p1;
contextSwitch();
}
static void createProcess(int priority) {
readyQueue.add(newProcess(processNo++, priority, "ready"));
}
static void contextSwitch() {
String sb;
String currentState = null;
sb = " " + currentlyRunning
+ "Currently Running Process"+"\n"
+ "Process No Priority Current State\n"
+ currentlyRunning+processNo
+ " "+currentlyRunning+priority+currentState + "\n\n";
currentState = "Blocked";
blockedQueue.add(currentlyRunning);
currentlyRunning = readyQueue.poll();
currentState = "Running";
sb = sb + "Ready Queue\n" + "Process No Priority Current State\n";
for (Process p : readyQueue) {
sb = sb + p+processNo + " " + p+priority + " " + p+currentState
+ "\n\n";
}
sb = sb + "\nBlocked Queue\n" + "Process No Priority Current State\n";
for (Process p : blockedQueue) {
sb = "\n"
+ sb + p+processNo + " " + p+priority + " " + p+currentState;
Queues.setText(sb);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newProcess) {
System.out.print("process");
int p = Integer.parseInt(priority.getText());
p = (p < 0) ? 2 : p;
createProcess(p);
} else{
System.out.print("context");
contextSwitch();
}
}
}
输出应该是这样的:click here 但是我得到了这样一个空白的gui:click here 当我编译代码时,我得到了很多例外。如果我输入一个数字并单击任何按钮,我就会得到未知来源。但是没有在GUI中做任何事情,这就是堆栈跟踪:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at dispatcher.newProcess(dispatcher.java:24)
at dispatcher.main(dispatcher.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
答案 0 :(得分:1)
您的代码是
public static void main(String args[]) {
dispatcher d = new dispatcher();
readyQueue = new PriorityQueue<>();
blockedQueue = new PriorityQueue<>();
Process p1 = newProcess(processNo++, 2, "Running"); // <--- HERE
...
在标记的行上,您正在调用方法newProcess()
。该方法目前被编码为抛出您所看到的异常。
private static Process newProcess(int i, int i0, String running) {
throw new UnsupportedOperationException("Not supported yet.");
}
换句话说,您的代码正在完全按照您的要求执行操作。
要解决此问题,您必须编写newProcess()
的代码。