import java.io.BufferedReader;
import java.io.FileReader;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Schduling_CPU_job {
private PriorityQueue<CPUscduller> pQueue = new PriorityQueue<CPUscduller>();
private static CPUscduller performJob = null;
public void addJobs(CPUscduller cpujob){
this.pQueue.add(cpujob);
}
public void CPU(){
int count = 1;
while(!pQueue.isEmpty()){
performJob = pQueue.remove();
for(int i = 0;i <= performJob.LengthOfpQueue-1;i++) {
if (count == 1) {
System.out.println("Job for time Slice# " + count + "-" + performJob.iName + "
arrived");
System.out.println(" Executing " + performJob.iName + " " +
performJob.iPriority + " " + (performJob.LengthOfpQueue));
count++;
} else {
System.out.println("Job for time Slice# " + count + " - Executing " +
performJob.iName + " " + performJob.iPriority + " " + (performJob.LengthOfpQueue -
count + 1));
count++;
}
}
}
}
public static void main(String[] args) throws Exception{
PriorityQueue pQueue = new PriorityQueue();
System.out.println("Enter your input file name");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
//System.out.println("input : " + input);
FileReader fileReader = new FileReader("./"+input);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while((line = bufferedReader.readLine()) != null){
Schduling_CPU_job sc = new Schduling_CPU_job();
String str[] = line.split(" ");
//System.out.println("\nThis is the context of input : " + line + "\n");
int inputqsize = Integer.parseInt(str[3]);
int priority = Integer.parseInt(str[2]);
int arrivaltime = Integer.parseInt(str[1]);
String jobname = str[0];
System.out.println("Length of job : "+inputqsize);
System.out.println("priority : " + priority);
System.out.println("Arrival time : "+ arrivaltime);
System.out.println("job name : " + jobname);
int length = inputqsize;
CPUscduller job = new CPUscduller(arrivaltime, priority, inputqsize, jobname);
sc.addJobs(job);
sc.CPU();
}
}
}
class CPUscduller implements Comparable<CPUscduller>{
int iPriority;
int arrivaltime;
int LengthOfpQueue;
String iName;
public CPUscduller(int arrivaltime,int iPriority,int iTimeSlice,String iName) throws
Exception{
if(iPriority >= -10 && iPriority <=10 ){
this.iPriority = iPriority;
}
else{
throw new Exception("Priority value is Out-of-Range");
}
if(iTimeSlice <=100 && iTimeSlice >= 1){
this.LengthOfpQueue = iTimeSlice;
}
else{
throw new Exception("Length is out of bounds");
}
this.iName = iName;
this.arrivaltime = arrivaltime;
}
public int getiPriority() {
return iPriority;
}
public void setiPriority(int iPriority) {
this.iPriority = iPriority;
}
public int getArrivaltime() {
return arrivaltime;
}
public void setArrivaltime(int arrivaltime) {
this.arrivaltime = arrivaltime;
}
public int getLengthOfpQueue() {
return LengthOfpQueue;
}
public void setLengthOfpQueue(int lengthOfpQueue) {
LengthOfpQueue = lengthOfpQueue;
}
public String getiName() {
return iName;
}
public void setiName(String iName) {
this.iName = iName;
}
public int compareTo(CPUscduller cpujob){
if(this.equals(cpujob)) return 0;
else if(getiPriority() > cpujob.getiPriority()) return 1;
else return -1;
//return (this.iPriority + cpujob.iPriority);
}
}
这是我编写的代码,但是由于某种原因它没有排序。我猜我在CPU方法上有问题,或者将我的输入值放在优先级队列中。这是因为在调试程序时,某些原因导致我的输入文件无法连续读取。无论如何,我的优先级值为-9,2,-10,10。最高优先级是-10,最低优先级是10。因此,排序应类似于-10,-9,2,10
这是我的输入文件示例。
Job114 0 -9 25
Job345 0 2 66
Job234 10 -10 5
Job999 27 10 56