首頁 > Java > java教程 > 主體

Java如何實作作業調度

WBOY
發布: 2023-05-25 17:37:31
轉載
1304 人瀏覽過

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如何實作作業調度的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板