进程调度算法之短进程优先(Java)
进程类:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
public class Process implements Comparable<Process> {
private String mName; //线程名 user
private int mState; //0代表就绪 //1代表执行 //2代表阻塞
private Date mSubmitTime; //提交时间 user
private Date mRunTime; //运行需要时间 user
private Date mBeginTime; //开始运行时间
private Date mEndTime; //运行结束时间
private long mTurnOverTime; //周转时间
private double mResponseRitio; //响应比
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
public Process(String name, String submitTime, String runTime) throws ParseException {
this.mName = name;
this.mSubmitTime = simpleDateFormat.parse(submitTime);
this.mRunTime = simpleDateFormat.parse(runTime);
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public int getmState() {
return mState;
}
public void setmState(int mState) {
this.mState = mState;
}
public Date getmSubmitTime() {
return mSubmitTime;
}
public void setmSubmitTime(Date mSubmitTime) {
this.mSubmitTime = mSubmitTime;
}
public Date getmRunTime() {
return mRunTime;
}
public void setmRunTime(Date mRunTime) {
this.mRunTime = mRunTime;
}
public Date getmBeginTime() {
return mBeginTime;
}
public void setmBeginTime(Date mBeginTime) {
this.mBeginTime = mBeginTime;
}
public Date getmEndTime() {
return mEndTime;
}
public void setmEndTime(Date mEndTime) {
this.mEndTime = mEndTime;
}
public long getmTurnOverTime() {
return mTurnOverTime;
}
public void setmTurnOverTime(long l) {
this.mTurnOverTime = l;
}
public double getmResponseRitio() {
return mResponseRitio;
}
public void setmResponseRitio(double mResponseRitio) {
this.mResponseRitio = mResponseRitio;
}
@Override
public String toString() {
return " 线程名" + this.getmName() + " 状态符" + this.getmState()
+ " 提交时间" + simpleDateFormat.format(this.getmSubmitTime())
+ " 运行时间" + simpleDateFormat.format(this.getmRunTime())
+ " 开始运行时间" + simpleDateFormat.format(this.getmBeginTime())
+ " 运行结束时间" + simpleDateFormat.format(this.getmEndTime())
+ " 周转时间" + this.getmTurnOverTime()
+ " 响应比" + this.getmResponseRitio();
}
public String toString2() {
return " 线程名" + this.getmName() + " 状态符" + this.getmState()
+ " 提交时间" + simpleDateFormat.format(this.getmSubmitTime())
+ " 运行时间" + simpleDateFormat.format(this.getmRunTime())
+ " 开始运行时间" + this.getmBeginTime()
+ " 运行结束时间" + this.getmEndTime()
+ " 周转时间" + this.getmTurnOverTime()
+ " 响应比" + this.getmResponseRitio();
}
@Override
public int compareTo(Process o) {
// TODO Auto-generated method stub
int y = (int) (o.mSubmitTime.getTime()/1000 + 8 * 60 * 60);
int x = (int) (this.mSubmitTime.getTime()/1000 + 8 * 60 * 60);
if (x != y) {
return (int)(x - y);
}
else {
return (int) ((this.mRunTime.getTime()/1000 + 8 * 60 * 60)
- (o.mRunTime.getTime()/1000 + 8 * 60 * 60));
}
}
}
//先按照提交时间排序,再按照运行时间排序
class sortByAll implements Comparator<Process>{
@Override
public int compare(Process o1, Process o2) {
int y = (int) (o2.getmSubmitTime().getTime()/1000 + 8 * 60 * 60);
int x = (int) (o1.getmSubmitTime().getTime()/1000 + 8 * 60 * 60);
if (x != y) {
return (int)(x - y);
}
else {
return (int) ((o1.getmRunTime().getTime()/1000 + 8 * 60 * 60)
- (o2.getmRunTime().getTime()/1000 + 8 * 60 * 60));
}
}
}
class sortBymRunTime implements Comparator<Process>{
@Override
public int compare(Process o1, Process o2) {
return (int) ((o1.getmRunTime().getTime()/1000 + 8 * 60 * 60)
- (o2.getmRunTime().getTime()/1000 + 8 * 60 * 60));
}
}
调度算法类:
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class ShortRuntime {
private static String threadName;
private static String threadSubmitTime;
private static String threadRunTime;
private static Date now; //记录当前时间
private static int i = 2;
public static void main(String[] args) throws ParseException {
//创建线程对象的集合
List<Process> list = new ArrayList<>();
//手动输入太麻烦了,先自己创建几个对象调试程序
list.add(new Process("Process1", "00:00:00", "00:02:20"));
list.add(new Process("Process2", "00:02:01", "00:03:20"));
list.add(new Process("Process3", "00:02:01", "00:00:20"));
list.add(new Process("Process4", "00:03:01", "00:01:20"));
list.add(new Process("Process5", "00:04:01", "00:00:50"));
list.add(new Process("Process6", "00:04:01", "00:00:20"));
list.add(new Process("Process7", "00:04:01", "00:10:50"));
list.add(new Process("Process8", "00:04:01", "00:10:50"));
list.add(new Process("Process9", "00:04:01", "00:00:50"));
list.add(new Process("Process10", "00:04:01", "00:01:50"));
list.add(new Process("Process11", "00:04:01", "00:22:50"));
list.add(new Process("Process12", "00:04:01", "01:00:50"));
startUp(list);
}
//最开始的时候先比较提交时间,在提交时间相同的情况下比较运行时间长短
//程序开始运行后,只要提交时间小于当前时间的进程,按运行时间长短执行
public static void startUp(List<Process> list) throws ParseException {
int flag = 0;
now = new Date(-8 * 60 * 60 * 1000);
System.out.println(now);
Collections.sort(list);
list.get(0).setmState(1);
list.get(0).setmBeginTime(list.get(0).getmSubmitTime());
list.get(0).setmEndTime(new Date(((list.get(0).getmBeginTime().getTime()/1000 + 8 * 60 * 60)
+ (list.get(0).getmRunTime().getTime()/1000)) * 1000)); //我也不知为什么要这么写,简直是够了
list.get(0).setmTurnOverTime((list.get(0).getmRunTime().getTime())/1000 + 8 * 60 * 60);
list.get(0).setmResponseRitio(1);
System.out.println("第1次执行进程:");
System.out.println(list.get(0).toString());
for (Process process : list) {
if(process.getmState() != 1) {
System.out.println(process.toString2());
}
}
now = new Date(now.getTime() + list.get(0).getmRunTime().getTime() + 8 * 60 * 60 * 1000);
//System.out.println(now);
//当第一个进程执行完毕之后,移除执行完毕的进程,并询问是否要添加新的进程
list.remove(0);
System.out.println("是否要添加新的进程,如果是请输入1,否请输入0");
Scanner scanner = new Scanner(System.in);
flag = scanner.nextInt();
if (flag == 1) {
addProcess(list);
}
else {
execute(list);
}
}
public static void addProcess(List<Process> list) throws ParseException {
int total = 0;
System.out.println("请按如下格式输入线程名,线程提交时间,需要运行时间。 "
+ "Example: Process1, 09:13:01, 11:00:20");
System.out.println("请输入需要创建线程个数");
Scanner scanner = new Scanner(System.in);
total = scanner.nextInt();
for (int i = 0; i < total; i++) {
//读取信息
System.out.println("请输入线程名");
threadName = scanner.next();
System.out.println("请输入线程提交时间(提交时间必须大于当前时间)");
threadSubmitTime = scanner.next();
System.out.println("请输入需要运行时间");
threadRunTime = scanner.next();
Process process = new Process(threadName, threadSubmitTime, threadRunTime);
list.add(process);
}
Collections.sort(list);
execute(list);
}
public static void execute(List<Process> list) throws ParseException {
while (list.isEmpty()) {
System.out.println("进程已经执行完毕");
System.exit(0);
}
double res = 0;//响应比
int flag = 0;
List<Process> newList = new ArrayList<>();
for (Process process : list) {
if((process.getmSubmitTime().getTime() + 28800000) < (now.getTime() + 28800000)) {
newList.add(process);
}
}
//System.out.println(list.get(0).getmSubmitTime().getTime() + 28800000);
Collections.sort(newList, new sortBymRunTime());
newList.get(0).setmState(1);
newList.get(0).setmBeginTime(now);
newList.get(0).setmEndTime(new Date(((now.getTime()/1000 + 8 * 60 * 60)
+ (newList.get(0).getmRunTime().getTime()/1000)) * 1000)); //我也不知为什么要这么写,简直是够了
newList.get(0).setmTurnOverTime(newList.get(0).getmEndTime().getTime()/1000
- newList.get(0).getmSubmitTime().getTime()/1000);
res = (double) (newList.get(0).getmTurnOverTime()/(double)(newList.get(0).getmRunTime().getTime()/1000 + 28800));
now = new Date(newList.get(0).getmEndTime().getTime());
newList.get(0).setmResponseRitio(res);
System.out.println("第" + i + "执行进程:");
i++;
System.out.println(newList.get(0).toString());
String name = newList.get(0).getmName();
Iterator<Process> iterator = list.iterator();
while (iterator.hasNext()) {
Process process = (Process) iterator.next();
if (process.getmName() == name) {
iterator.remove();
}
}
System.out.println("是否要添加新的进程,如果是请输入1,否请输入0");
Scanner scanner = new Scanner(System.in);
flag = scanner.nextInt();
if (flag == 1) {
addProcess(list);
}
else {
execute(list);
}
}
}
感觉自己写的有点复杂了,直接用String的二维数组会不会简单一些,接下来还要写一个关于进程调度的算法,就用它试试了。
附上运行结果: