Home > Java > javaTutorial > How to implement job scheduling in Java

How to implement job scheduling in Java

WBOY
Release: 2023-05-25 17:37:31
forward
1339 people have browsed it

Java implements job scheduling

Requirements

Java implements SJF algorithm scheduling, requiring that test data can be input immediately or read from a file;

must take into account the job Arrival time;

Finally be able to calculate the turnaround time and weighted turnaround time of each job, and add Chinese comments to the code

Reference code

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());
    }
}
Copy after login

Operation effect

How to implement job scheduling in Java

The above is the detailed content of How to implement job scheduling in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template