public static void main(String[] args) { // 初始化进程列表 List<Process> processes = new ArrayList<>(); processes.add(new Process("P1", 0, 0, 3)); processes.add(new Process("P2", 0, 2, 6)); processes.add(new Process("P3", 0, 4, 4)); processes.add(new Process("P4", 0, 6, 2)); processes.add(new Process("P5", 0, 8, 5)); int currentTime = 0; // 当前时间 int finishedCount = 0; // 已完成的进程数 Process runningProcess = null; // 当前正在运行的进程 while (finishedCount < processes.size()) { // 找到到达时间最早的进程 Process earliestProcess = null; for (Process process : processes) { if (process.status == 0 && process.arriveTime <= currentTime) { if (earliestProcess == null || process.arriveTime < earliestProcess.arriveTime) { earliestProcess = process; } } } if (earliestProcess != null) { // 运行进程 earliestProcess.status = 2; runningProcess = earliestProcess; System.out.println("时间 " + currentTime + " 运行进程 " + runningProcess.name + ",剩余时间 " + runningProcess.runTime); } else if (runningProcess != null) { // 继续运行进程 runningProcess.runTime--; System.out.println("时间 " + currentTime + " 继续运行进程 " + runningProcess.name + ",剩余时间 " + runningProcess.runTime); if (runningProcess.runTime == 0) { // 完成进程 runningProcess.status = 3; finishedCount++; System.out.println("时间 " + (currentTime + 1) + " 完成进程 " + runningProcess.name); runningProcess = null; } } currentTime++; } System.out.println("所有进程完成,总耗时 " + currentTime); } }什么意思
时间: 2024-03-14 09:48:51 浏览: 56
import java.io.*; public class FirstPart{ public static void main(String[] args) throws Exception{ System.out.print("The input Fahrenheit is "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int f = Integer.parseInt(br.re
5星 · 资源好评率100%
这段代码是一个简单的进程调度算法,使用的是先来先服务(FCFS)算法。在程序中,将进程存储在一个列表中,每个进程有四个属性:进程名、到达时间、开始时间和运行时间。程序先找到到达时间最早的进程,如果该进程已经到达,则运行它。如果当前没有进程在运行,则选择到达时间最早的进程开始运行。运行进程时,每个时间单位减少其运行时间,直到运行时间为零,则该进程完成。程序输出每个进程运行的开始时间,结束时间以及总耗时。
阅读全文