java实现作业调度要求java实现sjf算法调度,要求测试数据可以随即输入或从文件中读入;
必须要考虑到作业的到达时间;
最终能够计算每一个作业的周转时间、带权周转时间,给代码加中文注释
参考代码import java.util.arraylist;import java.util.comparator;import java.util.list;import java.util.random;public class test { static class job { public int jobid; public int arrivetime; public int needtime; public int finishtime; public int turnover; public double weightturnover; public job(int jobid, int arrivetime, int needtime) { this.jobid = jobid; this.arrivetime = arrivetime; this.needtime = needtime; } } public static void main(string[] args) { list<job> joblist = new arraylist<>(); random random = new random(); int jobsize = 5; int rangearrivetime = 5; int rangeneedtime = 10; for (int i = 0; i < jobsize; i++) { job job = new job(i, random.nextint(rangearrivetime), random.nextint(rangeneedtime) + 1); joblist.add(job); } joblist.sort(comparator.comparingint(o -> o.arrivetime)); int currenttime = 0; int totalturnover = 0; double totalweightturnover = 0; int completejobnum = 0; while (completejobnum < joblist.size()) { int shortestneedtime = integer.max_value; job shortestneedjob = null; for (job job : joblist) { if (job.finishtime > 0) { continue; } if (job.arrivetime <= currenttime && job.needtime < shortestneedtime) { shortestneedtime = job.needtime; shortestneedjob = job; } } currenttime += shortestneedjob.needtime; shortestneedjob.finishtime = currenttime; shortestneedjob.turnover = shortestneedjob.finishtime - shortestneedjob.arrivetime; shortestneedjob.weightturnover = (double) shortestneedjob.turnover / shortestneedjob.needtime; totalturnover += shortestneedjob.turnover; totalweightturnover += shortestneedjob.weightturnover; completejobnum++; } for (job job : joblist) { system.out.println("作业" + job.jobid + "的周转时间为" + job.turnover + ",带权周转时间为" + job.weightturnover); } system.out.println("平均周转时间为" + (double) totalturnover / joblist.size()); system.out.println("带权平均周转时间为" + totalweightturnover / joblist.size()); }}
运行效果
以上就是java如何实现作业调度的详细内容。