简体   繁体   中英

Trying to use priority queue and do not understand comparators

EDIT: I added in where I create new instance of the class Process as p. EDIT 2: added errors from compile.

Writting a program for class that simulates process scheduling and I am trying to use a priority queue that initially stores all of the processes before the specified submission time. I want to prioritize them in the notSubmitted priority queue by process priority. I think I need to use a comparator but I am not sure as I have never programmed with a priority queue before. This is what I am trying to do below. Can anyone help me with whats wrong and what I need to do to fix it?

I have a process class not show below, if you would like me to add that in I can.

creating a new instance of the Process class.

    static PriorityQueue<Process> notSubmitted = new PriorityQueue<Process>(25,
             new Comparator<Process>(){
                    public int compare(Object t1, Object t2)
                    {
                            int process1Time = ((Process)t1).getsubmissionTime();
                            int process2Time = ((Process)t2).getsubmissionTime();

                            if(process1Time > process2Time)
                            {
                                    return 1;
                            }
                            else if(process1Time < process2Time)
                            {
                                    return -1;
                            }
                            else
                            {
                                    return 0;
                            }
                    }

    } );

            Process[] p = new Process[25];

This is where I populate the queue. (initially I had a regular queue and everything worked fine, but I want to change to a priority queue to make it a little easier later on in the program)

for(int j = 0; j < numProcesses; ++j)
            {
                    pid = i.nextInt();
                    priority = i.nextInt();
                    submissionTime = i.nextInt();
                    totalCpuTime = i.nextInt();
                    computeTime = i.nextInt();
                    ioTime = i.nextInt();
                    p[j] = new Process(pid, priority, submissionTime, totalCpuTime, computeTime, ioTime);
            }
            System.out.printf("%d  %d  %d\n",c.getcpuNum(), c.getnumProcesses(), c.getqSize());

            for(int r = 0; r < numProcesses; ++r)
            {
                    //populate the not submitted queue first
                    notSubmitted.add(p[r]);
            }

Compile error

javac prog2.java
prog2.java:84: <anonymous prog2$1> is not abstract and does not override abstract method compare(Process,Process) in java.util.Comparator
                    public int compare(Object t1, Object t2)
                               ^
1 error

Your compare method needs to take two Process arguments, not two Object s.

...
public int compare(Process p1, Process p2) {
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM