定义进程工作对象
class Job {
String name;
//到达时间
int arrivalTime;
//执行时间
int executionTime;
public Job(String name, int arrivalTime, int executionTime) {
this.name = name;
this.arrivalTime = arrivalTime;
this.executionTime = executionTime;
}
}
测试方法
public static void fcfs_run() {
// 创建作业列表
List<Job> jobs = new ArrayList<>();
jobs.add(new Job("Job1", 3, 10));
jobs.add(new Job("Job2", 2, 10));
jobs.add(new Job("Job3", 1, 10));
// 按照到达时间进行排序
jobs.sort(Comparator.comparingInt(j -> j.arrivalTime));
// 周转时间=完成时间-到达时间;
int finishTotal = 0;
int turnaroundTimeTotal = 0;
for (Job job : jobs) {
//完成
finishTotal += job.arrivalTime + job.executionTime;
// 计算周转时间
int turnaroundTime = finishTotal - job.arrivalTime;
// 更新周转时间总和
turnaroundTimeTotal += turnaroundTime;
System.out.println("作业 " + job.name + " 周转时间:" + turnaroundTime);
}
double averageTurnaroundTime = (double) turnaroundTimeTotal / jobs.size();
BigDecimal bd = new BigDecimal(averageTurnaroundTime);
// 四舍五入
BigDecimal roundedNumber = bd.setScale(2, RoundingMode.HALF_UP);
System.out.println("平均周转时间:" + roundedNumber.doubleValue());
}