Search code examples
javaoperating-systempriority-queue

Dispatcher with priority queue throwing exceptions


I'm trying to run an example of a dispatcher with a priority queue system. It works as described below:

New processes are entered using a GUI with priority included. Processes are also terminated by GUI command. Context switches are by command with the cause of the switch being immaterial. Three components are used in the code including: Priority based Ready Queue(s), Blocked list and Output of complete system status after every context switch showing ready, blocked, and running processes.

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

The output should be something like this: click here But I'm getting a blank gui like this: click here When I compile the code, I'm getting a lot of exceptions. If I enter a number and click any button, I'm getting unknown sources. But without doing anything in the GUI, this is the 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)

Solution

  • Your code is

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

    At the line marked you are calling your method newProcess(). That method is currently coded to throw the exception you are seeing.

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

    In other words, your code is doing EXACTLY what you told it to do.

    To resolve this problem you must write the code for newProcess().