2016-07-25 6 views
-2

Ich versuche, ein Beispiel für einen Dispatcher mit einem Prioritätswarteschlangensystem auszuführen. Es funktioniert wie folgt:Dispatcher mit Prioritätswarteschleifen, die Ausnahmen auslösen

Neue Prozesse werden über eine GUI mit Priorität eingegeben. Prozesse werden auch durch einen GUI-Befehl beendet. Kontextwechsel sind per Befehl, wobei die Ursache des Schalters unwesentlich ist. Im Code werden drei Komponenten verwendet: Prioritätsbasierte Bereit-Warteschlange (n), Blockierte Liste und Ausgabe des vollständigen Systemstatus nach jedem Kontextwechsel, der bereit, blockierte und laufende Prozesse anzeigt.

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(); 
     } 
    } 
} 

sollte die Ausgabe etwa wie folgt sein: click here Aber ich bin eine leere gui wie diese bekommen: click here Wenn ich den Code zu kompilieren, ich bin eine Menge Ausnahmen zu bekommen. Wenn ich eine Nummer eingebe und auf eine beliebige Schaltfläche klicke, bekomme ich unbekannte Quellen. Aber ohne in der GUI etwas zu tun, das ist der Stack-Trace:

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) 
+1

_ "Wenn ich den Code kompiliere, bekomme ich viele Ausnahmen" _ - Sie erhalten keine Ausnahmen, wenn Sie Code kompilieren, erhalten Sie Compiler-Fehlermeldungen. Zeigen Sie diese Nachrichten an. –

+0

Ich habe den Stack-Trace für die Kompilierungsfehler hinzugefügt, danke – codingenthusiast

+0

@JimGarrison Ich postete die Fehler, wenn jemand einen Blick darauf werfen kann, würde es geschätzt werden – codingenthusiast

Antwort

1

Ihr Code ist

public static void main(String args[]) { 
    dispatcher d = new dispatcher(); 
    readyQueue = new PriorityQueue<>(); 
    blockedQueue = new PriorityQueue<>(); 
    Process p1 = newProcess(processNo++, 2, "Running"); // <--- HERE 
    ... 

Auf der Linie markieren Sie Ihre Methode newProcess() anrufen. Diese Methode ist derzeit so codiert, dass sie die Ausnahme auslöst, die Sie sehen.

private static Process newProcess(int i, int i0, String running) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

Mit anderen Worten, Ihr Code tut GENAU, was Sie ihm gesagt haben. Um dieses Problem zu beheben, müssen Sie den Code für newProcess() schreiben.

Verwandte Themen