先来先服务+优先级调度+短作业优先调度+响应比优先调度
代码
java">import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;class Main {static class tasks{int id;//序号char jinchengname;//进程名int jinchengId;//double arriveTime;//提交时间double runningTime;//运行时间int youxianshu;//优先数double jinrushuchujingshijian;//进入输出#时间double startRunningTime;//开始运行时间double shangxvyunxingshijian;//尚需运行时间double runningEndTime;//运行结束时间double zhouzhuanshijian;//周转时间double saiquanzhouzhuanshijian;//带权周转时间int order;//运行次序double xingyingbi;//响应比public int getId() {return id;}public void setId(int id) {this.id = id;}public char getJinchengname() {return jinchengname;}public void setJinchengname(char jinchengname) {this.jinchengname = jinchengname;}public int getJinchengId() {return jinchengId;}public void setJinchengId(int jinchengId) {this.jinchengId = jinchengId;}public double getArriveTime() {return arriveTime;}public void setArriveTime(double arriveTime) {this.arriveTime = arriveTime;}public double getRunningTime() {return runningTime;}public void setRunningTime(double runningTime) {this.runningTime = runningTime;}public int getYouxianshu() {return youxianshu;}public void setYouxianshu(int youxianshu) {this.youxianshu = youxianshu;}public double getJinrushuchujingshijian() {return jinrushuchujingshijian;}public void setJinrushuchujingshijian(double jinrushuchujingshijian) {this.jinrushuchujingshijian = jinrushuchujingshijian;}public double getStartRunningTime() {return startRunningTime;}public void setStartRunningTime(double startRunningTime) {this.startRunningTime = startRunningTime;}public double getShangxvyunxingshijian() {return shangxvyunxingshijian;}public void setShangxvyunxingshijian(double shangxvyunxingshijian) {this.shangxvyunxingshijian = shangxvyunxingshijian;}public double getRunningEndTime() {return runningEndTime;}public void setRunningEndTime(double runningEndTime) {this.runningEndTime = runningEndTime;}public double getZhouzhuanshijian() {return zhouzhuanshijian;}public void setZhouzhuanshijian(double zhouzhuanshijian) {this.zhouzhuanshijian = zhouzhuanshijian;}public double getSaiquanzhouzhuanshijian() {return saiquanzhouzhuanshijian;}public void setSaiquanzhouzhuanshijian(double saiquanzhouzhuanshijian) {this.saiquanzhouzhuanshijian = saiquanzhouzhuanshijian;}public int getOrder() {return order;}public void setOrder(int order) {this.order = order;}public tasks(int id, char jinchengname, int jinchengId, double arriveTime, double runningTime, int youxianshu) {this.id = id;this.jinchengname = jinchengname;this.jinchengId = jinchengId;this.arriveTime = arriveTime;this.runningTime = runningTime;this.youxianshu = youxianshu;}public double getXingyingbi() {return xingyingbi;}public void setXingyingbi(double xingyingbi) {this.xingyingbi = xingyingbi;}}public static void main(String[] args) {List<tasks> tasksList=new ArrayList<>();System.out.println("请输入task个数");Scanner sc=new Scanner(System.in);int n=sc.nextInt();for (int i = 0; i < n; i++) {System.out.println("请依次输入第"+(i+1)+"个进程的序号 进程名 进程号 到达时间 运行时间 优先数");int id=sc.nextInt();char jingchengname=sc.next().charAt(0);int jingchengid= sc.nextInt();double arrivetime= sc.nextDouble();double runningtime= sc.nextDouble();int youxianshu= sc.nextInt();tasksList.add(new tasks(id,jingchengname,jingchengid,arrivetime,runningtime,youxianshu));}//选择调度System.out.println("1.先来先服务");System.out.println("2.优先级调度");System.out.println("3.短作业优先调度");System.out.println("4.响应比高者优先调度");int ch=sc.nextInt();switch (ch){case 1:xianlaixianfuwu(tasksList);break;case 2:youxianjidiaodu(tasksList);break;case 3:duanzuoyeyouxian(tasksList);break;case 4:xiangyingbigaoyouxian(tasksList);break;}}public static void xianlaixianfuwu(List<tasks> tasksList) {//先来先服务Comparator<tasks> comparator=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator);int order=0;double time=tasksList.get(0).arriveTime;//此时时间点for (tasks tasks : tasksList) {order++;tasks.setOrder(order);if(time>= tasks.arriveTime){tasks.startRunningTime=time;}else {time=tasks.arriveTime;//这里少些一句这个else 才看到 补上了}time+= tasks.runningTime;tasks.runningEndTime=time;tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;tasks.saiquanzhouzhuanshijian=tasks.zhouzhuanshijian / tasks.runningTime;System.out.println("序号\t"+"进程名\t"+"进程号\t"+"到达时间\t"+"运行时间\t"+"优先数\t"+"开始运行时间\t"+"运行结束时间\t"+"周转时间\t"+"带权周转时间(左->右)");System.out.println(tasks.id+" "+tasks.jinchengname+" "+tasks.jinchengId+" "+ tasks.arriveTime+" "+ tasks.runningTime+" "+ tasks.youxianshu+" "+tasks.startRunningTime+" "+ tasks.runningEndTime+" "+tasks.zhouzhuanshijian+" "+tasks.saiquanzhouzhuanshijian+" "+ tasks.getOrder());}}public static void youxianjidiaodu(List<tasks> tasksList) {List<tasks> tasksyiwanchengList=new ArrayList<>();//找最先的到达的作为时间time的开始,完成后更新time+=runningtimeComparator<tasks> comparator=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator);//tasksyiwanchangList.add(task001)tasksyiwanchengList.add(tasksList.get(0));double time=tasksList.get(0).arriveTime;//此时时间点tasksList.get(0).startRunningTime=time;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;//tasksList.del(task001)删除最先的到达的tasktasksList.remove(0);//list.for执行下面这行的操作//在剩下的task里&&arrivetime<time也就是已经到达但还没开始的任务里,选执行优先级最高的执行,更新time+=runningtimeint n= tasksList.size();for (int i = 0; i < n; i++) {//每次for里都要完成且只完成一个任务//将未完成的tasks按优先数排序Comparator<tasks> comparator2=Comparator.comparing(tasks:: getYouxianshu);tasksList.sort(comparator2);int flag=0;for (tasks tasks : tasksList) {if(tasks.arriveTime<=time){tasks.startRunningTime=time;time+=tasks.runningTime;tasks.runningEndTime=time;tasksyiwanchengList.add(tasks);tasksList.remove(tasks);flag=1;break;}}if (flag==0){//如果全都没到达,直接移除接下来最早到达的Comparator<tasks> comparator3=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator3);tasksList.get(0).startRunningTime=tasksList.get(0).arriveTime;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;tasksyiwanchengList.add(tasksList.get(0));tasksList.remove(0);}}//最后遍历出结果tasksyiwanchengListint order=0;for (tasks tasks : tasksyiwanchengList) {order++;tasks.setOrder(order);tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;tasks.saiquanzhouzhuanshijian=tasks.zhouzhuanshijian / tasks.runningTime;System.out.println("序号\t"+"进程名\t"+"进程号\t"+"到达时间\t"+"运行时间\t"+"优先数\t"+"开始运行时间\t"+"运行结束时间\t"+"周转时间\t"+"带权周转时间(左->右)");System.out.println(tasks.id+" "+tasks.jinchengname+" "+tasks.jinchengId+" "+ tasks.arriveTime+" "+ tasks.runningTime+" "+ tasks.youxianshu+" "+tasks.startRunningTime+" "+ tasks.runningEndTime+" "+tasks.zhouzhuanshijian+" "+tasks.saiquanzhouzhuanshijian+" "+ tasks.getOrder());}}public static void duanzuoyeyouxian(List<tasks> tasksList) {List<tasks> tasksyiwanchengList=new ArrayList<>();//找最先的到达的作为时间time的开始,完成后更新time+=runningtimeComparator<tasks> comparator=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator);//tasksyiwanchangList.add(task001)tasksyiwanchengList.add(tasksList.get(0));double time=tasksList.get(0).arriveTime;//此时时间点tasksList.get(0).startRunningTime=time;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;//tasksList.del(task001)删除最先的到达的tasktasksList.remove(0);//list.for执行下面这行的操作//在剩下的task里&&arrivetime<time也就是已经到达但还没开始的任务里,选作业runningtime最小的执行,更新time+=runningtimeint n= tasksList.size();for (int i = 0; i < n; i++) {//每次for里都要完成且只完成一个任务//将未完成的tasks按作业长短排序Comparator<tasks> comparator2=Comparator.comparing(tasks:: getRunningTime);//那这么的话和优先级相比只改了这一个单词tasksList.sort(comparator2);int flag=0;for (tasks tasks : tasksList) {if(tasks.arriveTime<=time){tasks.startRunningTime=time;time+=tasks.runningTime;tasks.runningEndTime=time;tasksyiwanchengList.add(tasks);tasksList.remove(tasks);flag=1;break;}}if (flag==0){//如果全都没到达,直接移除接下来最早到达的Comparator<tasks> comparator3=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator3);tasksList.get(0).startRunningTime=tasksList.get(0).arriveTime;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;tasksyiwanchengList.add(tasksList.get(0));tasksList.remove(0);}}//最后遍历出结果tasksyiwanchengListint order=0;for (tasks tasks : tasksyiwanchengList) {order++;tasks.setOrder(order);tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;tasks.saiquanzhouzhuanshijian=tasks.zhouzhuanshijian / tasks.runningTime;System.out.println("序号\t"+"进程名\t"+"进程号\t"+"到达时间\t"+"运行时间\t"+"优先数\t"+"开始运行时间\t"+"运行结束时间\t"+"周转时间\t"+"带权周转时间(左->右)");System.out.println(tasks.id+" "+tasks.jinchengname+" "+tasks.jinchengId+" "+ tasks.arriveTime+" "+ tasks.runningTime+" "+ tasks.youxianshu+" "+tasks.startRunningTime+" "+ tasks.runningEndTime+" "+tasks.zhouzhuanshijian+" "+tasks.saiquanzhouzhuanshijian+" "+ tasks.getOrder());}}public static void xiangyingbigaoyouxian(List<tasks> tasksList) {List<tasks> tasksyiwanchengList=new ArrayList<>();//响应比高优先 当每次进程被调度就要重新计算新的响应比//找最先的到达的作为时间time的开始,完成后更新time+=runningtimeComparator<tasks> comparator=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator);//tasksyiwanchangList.add(task001)tasksyiwanchengList.add(tasksList.get(0));double time=tasksList.get(0).arriveTime;//此时时间点tasksList.get(0).startRunningTime=time;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;//tasksList.del(task001)删除最先的到达的tasktasksList.remove(0);//list.for执行下面这行的操作//在剩下的task里&&arrivetime<time也就是已经到达但还没开始的任务里,选执行响应比最高的执行,更新time+=runningtimeint n= tasksList.size();for (int i = 0; i < n; i++) {//每次for里都要完成且只完成一个任务 然后响应比改变 重新计算新的响应比//计算runningendtime 迭代每个任务是下一次就被调的情况得到的tasks.runningendtimefor (tasks tasks : tasksList) {//好像应该time和arrivetime比较 还没到的要不要算响应比去比较啊??//先按照还没来的就不参与响应比的计算tasks.xingyingbi=-1;//为了计算响应比才暂时赋值 后面每次for里都要完成一个任务时才是正式的赋值if (tasks.arriveTime<=time){//给来的任务计算响应比 不来的响应比默认-1tasks.runningEndTime+=(time+ tasks.runningTime);}}//每次重新计算响应比 所以没个任务的zhouzhuanshijian必须在下面的计算前就已经知道tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;for (tasks tasks : tasksList) {tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;//arrivetime已知不用管 runningendtime必须知道tasks.xingyingbi=1+(tasks.zhouzhuanshijian/ tasks.runningTime);}//将未完成的tasks按响应比排序Comparator<tasks> comparator2=Comparator.comparing(tasks:: getXingyingbi).reversed();//在这之前必须把响应比算出来tasksList.sort(comparator2);int flag=0;for (tasks tasks : tasksList) {if(tasks.arriveTime<=time){tasks.startRunningTime=time;time+=tasks.runningTime;tasks.runningEndTime=time;tasksyiwanchengList.add(tasks);tasksList.remove(tasks);flag=1;break;}}if (flag==0){//如果全都没到达,直接移除接下来最早到达的Comparator<tasks> comparator3=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator3);tasksList.get(0).startRunningTime=tasksList.get(0).arriveTime;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;tasksyiwanchengList.add(tasksList.get(0));tasksList.remove(0);}}//最后遍历出结果tasksyiwanchengListint order=0;for (tasks tasks : tasksyiwanchengList) {order++;tasks.setOrder(order);tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;//arrivetime已知不用管 runningendtime必须知道tasks.saiquanzhouzhuanshijian=tasks.zhouzhuanshijian / tasks.runningTime;System.out.println("序号\t"+"进程名\t"+"进程号\t"+"到达时间\t"+"运行时间\t"+"优先数\t"+"开始运行时间\t"+"运行结束时间\t"+"周转时间\t"+"带权周转时间(左->右)");System.out.println(tasks.id+" "+tasks.jinchengname+" "+tasks.jinchengId+" "+ tasks.arriveTime+" "+ tasks.runningTime+" "+ tasks.youxianshu+" "+tasks.startRunningTime+" "+ tasks.runningEndTime+" "+tasks.zhouzhuanshijian+" "+tasks.saiquanzhouzhuanshijian+" "+ tasks.getOrder());}}
}
运行结果