开发者

Java实现作业调度的示例代码

开发者 https://www.devze.com 2023-04-04 11:03 出处:网络 作者: 绘绘~
目录Java实现作业调度要求参考代码运行效果Java实现作业调度 要求 Java实现SJF算法调度,要求测试数据可以随即输入或从文件中读入;
目录
  • 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 to开发者_Python培训talWeightTurnover = 0;  
        int completeJobNum = 0;  

        while (completeJobNum < jobList.size()) {
            int shortestNeedTime = Integer.MAX_VALUE;
            JobIYMfX shortestNeedJob = null;
            
            for (Job job : jobList) {
                if (job.finishTime > 0) { 
                    continue;
                }
                if (job.arrivewww.devze.comTime <= 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;  
            totalWeightTupythonrnover += shortestNeedJob.weightTurnover;  
            completeJobNum++;  
        }

        for (Job job : jobList) {
            System.out.println("作业" + job.jobId + "的周转时间为" + job.turnover + ",带权周转时间为" + job.weightTurnover);
        }

        System.out.println("平均周转时间为" python+ (doubl编程e) totalTurnover / jobList.size());
        System.out.println("带权平均周转时间为" + totalWeightTurnover / jobList.size());
    }
}

运行效果

Java实现作业调度的示例代码

到此这篇关于Java实现作业调度的示例代码的文章就介绍到这了,更多相关Java作业调度内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号