Java如何实现作业调度
短信预约 -IT技能 免费直播动态提醒
这篇文章主要讲解了“Java如何实现作业调度”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java如何实现作业调度”吧!
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如何实现作业调度”的内容了,经过本文的学习后,相信大家对Java如何实现作业调度这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341