1、首先先写一个实体类来表示进程
package demo;
public class Progress {
//到达时间
public int arriveTime;
//运行时间
public int runTime;
//完成时间
public int finishTime;
//周转时间
public int totalTime;
//带权周转时间
public float weightTotalTime;
//进程名
public String name;
public Progress(String name,int arriveTime,int runTime){
this.name = name;
this.arriveTime = arriveTime;
this.runTime = runTime;
}
//获得周转时间
public void getTotalTime(){
totalTime = finishTime - arriveTime;
}
//获得带权周转时间
public void getWeightTotalTime(){
weightTotalTime = (float)totalTime/runTime;
}
//打印结果
@Override
public String toString() {
return name+"到达时间:"+arriveTime+"运行时间:"+runTime+"完成时间:"+finishTime+"周转时间:"+totalTime+"带权周转时间:"+weightTotalTime;
}
}
2、将需要处理的进程存放到progressList中
package demo;
import java.util.ArrayList;
public class ProgressList {
//存放进程
private static ArrayList<Progress> progressList;
public ArrayList<Progress> getProgressList() {
return progressList;
}
//将四个进程存放到progressList中
static {
progressList = new ArrayList<Progress>();
progressList.add(new Progress("P1",0,3));
progressList.add(new Progress("P2",1,5));
progressList.add(new Progress("P3",2,2));
progressList.add(new Progress("P4",3,3));
}
}
3、实现两个算法
package demo;
import java.util.ArrayList;
public class Service {
private ArrayList<Progress> progressList;
//初始化进程
public void init() {
progressList = new ProgressList().getProgressList();
for(Progress p:progressList) {
p.finishTime = 0;
p.totalTime = 0;
p.weightTotalTime = 0;
}
}
//先到先服务
public ArrayList<Progress> FCFS() {
int time = 0;
for(int i=0;i<progressList.size();i++) {
int flag = progressList.size();
int compare = 100;
for(Progress p:progressList) {
if(p.finishTime==0) {
if(p.arriveTime<compare) {
compare = p.arriveTime;
flag = progressList.indexOf(p);
}
}
}
//更新进程
progressList.get(flag).finishTime = time + progressList.get(flag).runTime;
progressList.get(flag).getTotalTime();
progressList.get(flag).getWeightTotalTime();
//更新时间
time = progressList.get(flag).finishTime;
}
return progressList;
}
//短作业优先
public ArrayList<Progress> SJF(){
int time = 0;
for(int i=0;i<progressList.size();i++) {
int flag = progressList.size();
int compare = 100;
for(Progress p:progressList) {
//判断此时该进程是否到达
if(p.arriveTime<=time) {
//判断该进程是否已完成
if(p.finishTime==0) {
//判断该进程运行时间是否最短
if(p.runTime<compare) {
compare = p.runTime;
flag = progressList.indexOf(p);
}
}
}
}
//更新进程
progressList.get(flag).finishTime = time + progressList.get(flag).runTime;
progressList.get(flag).getTotalTime();
progressList.get(flag).getWeightTotalTime();
//更新时间
time = progressList.get(flag).finishTime;
}
return progressList;
}
}
4、主方法
package demo;
import java.util.ArrayList;
public class CPU {
public static void main(String[] args) {
Service service = new Service();
//先到先服务
service.init();
ArrayList<Progress> list = service.FCFS();
System.out.println("先到先服务的执行过程:");
for(Progress p:list) {
System.out.println(p);
}
//短作业优先
service.init();
list = service.SJF();
System.out.println("短作业优先的执行过程:");
for(Progress p:list) {
System.out.println(p);
}
}
}
程序截图
程序有些不完善的地方、欢迎修改