Java版操作系统先来先服务和高响应比算法
package com.dualseason.pojo;
/**
* @author dualseason
*/
public class Process {
//进程名
private String processName;
/**
* 到达时间
*/
private long processCreateTime = System.currentTimeMillis();
/**
* 服务时间
*/
private double ServiceTime;
/**
* 等待时间
*/
private double WaitTime;
private double Response;
public double getResponse() {
return Response;
}
public void setResponse(double response) {
Response = response;
}
public String getProcessName() {
return processName;
}
public void setProcessName(String processName) {
processName = processName;
}
public double getProcessCreateTime() {
return processCreateTime;
}
public double getServiceTime() {
return ServiceTime;
}
public void setServiceTime(int serviceTime) {
ServiceTime = serviceTime;
}
public double getWaitTime() {
return WaitTime;
}
public void setWaitTime(int waitTime) {
WaitTime = waitTime;
}
@Override
public String toString() {
return "Process{" +
"ProcessName='" + processName + '\'' +
", ProcessCreateTime=" + processCreateTime +
", ServiceTime=" + ServiceTime +
", WaitTime=" + WaitTime +
", Response=" + Response +
'}';
}
}
package com.dualseason.model;
import com.dualseason.pojo.Process;
import java.util.Scanner;
/**
* @author dualseason
*/
public class CreateProcess {
public static Process CreateProcess(){
Process process = new Process();
System.out.println("请输入进程名称--进程运行所需时间");
Scanner scanner = new Scanner(System.in);
String ProcessName = scanner.next();
String ProcessServiceTime = scanner.next();
process.setProcessName(ProcessName);
process.setServiceTime(Integer.parseInt(ProcessServiceTime));
return process;
}
}
package com.dualseason.result;
import com.dualseason.model.CreateProcess;
import com.dualseason.pojo.Process;
import java.util.*;
/**
* @author dualseason
*/
public class FCFSandHRRN {
public static void main(String[] args) {
//1.创建进程队列
ArrayList<Process> ProcessList = new ArrayList<>();
//2.创建新的进程
int num;
System.out.println("输入进程数");
Scanner scanner = new Scanner(System.in);
num = scanner.nextInt();
Process process = null;
for (int i = 0; i < num; i++) {
process = CreateProcess.CreateProcess();
//3.设置当前进程所需等待时间
int WaitTime = 0;
for (Process process1 : ProcessList) {
WaitTime += process1.getServiceTime();
}
process.setWaitTime(WaitTime);
process.setResponse(1+(double)process.getWaitTime()/process.getServiceTime());
ProcessList.add(process);
}
//使用switch进行排序选择
System.out.println("输入排序算法序号:");
System.out.println("0.先来先服务(and 其他)");
System.out.println("1.短作业优先算法");
System.out.println("2.高响应比优先算法");
int selector = scanner.nextInt();
switch(selector){
case 0:Collections.sort(ProcessList, (o1, o2) -> (int) ((o1.getProcessCreateTime()-o2.getProcessCreateTime())*10000000));break;
case 1:Collections.sort(ProcessList, (o1, o2) -> (int) ((o2.getWaitTime()-o1.getWaitTime())*10000000));break;
case 2:Collections.sort(ProcessList, (o1, o2) -> (int) ((o2.getResponse()-o1.getResponse())*10000000));break;
default:
System.out.println("非法输入");
}
//排序后输出
System.out.println("进程处理顺序:");
for (Process process1 : ProcessList) {
System.out.println(process1);
}
}
}