package test.example;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @Corperation:福建XX网络有限公司
* @ClassName: DispatchAlgorithm
* @Description: FCFS:从后备作业队伍中选择一个或者多个先进入
队列的作业,为其分 配资源,创建进程,
* 然后放入就绪队列,次
算法有利于长作业,不利于短作业。 SJF :
* 从后备队列中选择一个或者若干个估计运行时间最短的作业,将
他们调入
内存。
* @author: Topkinghat
* @date 2010-11-16 下午02:54:10
*/
public class DispatchAlgorithm {
private int arriveTime;// 到达时间
private int serviceTime;// 服务时间
private int finishTime;// 完成时间
private int roundTime;// 周转时间
private float withWeightRoundTime;// 带权周转时间
private float averageRoundTime;// 平均周转时间
private float averageWithWeightTime;// 平均带权周转时间
private boolean excuteStatus;// 当前的进程是否是执行
public DispatchAlgorithm() {
super();
this.arriveTime = 0;
this.serviceTime = 0;
this.finishTime = 0;
this.roundTime = 0;
this.withWeightRoundTime = 0;
this.averageRoundTime = 0;
this.averageWithWeightTime = 0;
this.excuteStatus = false;
}
/**
* public int getArriveTime() {
* return arriveTime;
* }
*
* public void setArriveTime(int arriveTime) {
* this.arriveTime = arriveTime;
* }
*
* public int get
ServiceTime() {
* return serviceTime;
* }
*
* public void setServiceTime(int serviceTime) {
* this.serviceTime = serviceTime;
* }
*/
/**
* @Description: FCFS各个时间关系运算
* @param
* @return void
* @throws
*/
public void doFCFS(DispatchAlgorithm lastObject,
DispatchAlgorithm nextObject) {
nextObject.finishTime = lastObject.finishTime + nextObject.serviceTime;
nextObject.roundTime = nextObject.finishTime - nextObject.arriveTime;
nextObject.withWeightRoundTime = (float) nextObject.roundTime
/ nextObject.serviceTime;
}
/**
* @Description: SJP各个时间关系运算
* @param
* @return void
* @throws
*/
public void doSJP(DispatchAlgorithm lastObject, DispatchAlgorithm nextObject) {
doFCFS(lastObject, nextObject);
}
/**
* @Description: 在上一个进程执行完毕后,查看已经到达的进程,比较其服务时间 返回服务小的进程
* @param
* @return DispatchAlgorithm
* @throws
*/
public DispatchAlgorithm doJudgeBeforSJP(DispatchAlgorithm last,
DispatchAlgorithm[] object) {
int minServiceTimePos = 0;
int minServieceTime = 0;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i < object.length; i++) {
if ((object[i].arriveTime <= last.finishTime)
&& (object[i].excuteStatus == false)) {
map.put(i, object[i].serviceTime);
list.add(i);
}
}
minServieceTime = object[list.get(0)].serviceTime;
minServiceTimePos = list.get(0);
for (int m = 1; m < list.size(); m++) {
if (map.get(list.get(m)) < minServieceTime) {
minServieceTime = map.get(list.get(m));
minServiceTimePos = list.get(m);
}
}
object[minServiceTimePos].excuteStatus = true;
return object[minServiceTimePos];
}
/**
* 打印出计算的结果
*/
public void printArriveTime(DispatchAlgorithm[] object) {
for (int i = 0; i < object.length; i++)
System.out.print(" " + object[i].arriveTime + " ");
}
public void printServiceTime(DispatchAlgorithm[] object) {
for (int i = 0; i < object.length; i++)
System.out.print(" " + object[i].serviceTime + " ");
}
public void printFinishTime(DispatchAlgorithm[] object) {
for (int i = 0; i < object.length; i++)
System.out.print(" " + object[i].finishTime + " ");
}
public void printRoundTime(DispatchAlgorithm[] object) {
for (int i = 0; i < object.length; i++)
System.out.print(" " + object[i].roundTime + " ");
}
public void printWithWeightTime(DispatchAlgorithm[] object) {
for (int i = 0; i < object.length; i++)
System.out.print(" " + object[i].withWeightRoundTime + " ");
}
public float printAverageRoundTime(DispatchAlgorithm[] object) {
int objectArraySize = object.length;
int tempSum = 0;
for (int i = 0; i < objectArraySize; i++) {
tempSum+=object[i].roundTime;
}
averageRoundTime=tempSum / objectArraySize;
return averageRoundTime;
}
public float printAverageWithWeightTime(DispatchAlgorithm[] object) {
int objectArraySize = object.length;
float tempSum = 0;
for (int i = 0; i < objectArraySize; i++) {
tempSum+=object[i].withWeightRoundTime;
}
withWeightRoundTime = tempSum / objectArraySize;
return withWeightRoundTime;
}
/**
* @Description: main函數
* @param
* @return void
* @throws
*/
public static void main(String[] args) {
// 创建进程对象以及初始化
DispatchAlgorithm[] object = new DispatchAlgorithm[5];
DispatchAlgorithm defaultObject = new DispatchAlgorithm();
for (int i = 0; i < 5; i++) {
object[i] = new DispatchAlgorithm();
}
DispatchAlgorithm A = object[0];
DispatchAlgorithm B = object[1];
DispatchAlgorithm C = object[2];
DispatchAlgorithm D = object[3];
DispatchAlgorithm E = object[4];
A.setArriveTime(0);
A.setServiceTime(4);
B.setArriveTime(1);
B.setServiceTime(3);
C.setArriveTime(2);
C.setServiceTime(5);
D.setArriveTime(3);
D.setServiceTime(2);
E.setArriveTime(4);
E.setServiceTime(4);
System.out.println("--------现在正在使用的是FCFS调度算法-------------");
A.doFCFS(defaultObject, A);
B.doFCFS(A, B);
C.doFCFS(B, C);
D.doFCFS(C, D);
E.doFCFS(D, E);
System.out.println(" A B C D E");
System.out.print("到达时间 ");
defaultObject.printArriveTime(object);
System.out.println();
System.out.print("服务时间 ");
defaultObject.printServiceTime(object);
System.out.println();
System.out.print("完成时间 ");
defaultObject.printFinishTime(object);
System.out.println();
System.out.print("周转时间 ");
defaultObject.printRoundTime(object);
System.out.println();
System.out.print("带权周转时间 ");
defaultObject.printWithWeightTime(object);
System.out.println();
System.out.print("平均周转时间 "+defaultObject.printAverageRoundTime(object));
System.out.println();
System.out.print("平均带权周转时间 "+defaultObject.printAverageWithWeightTime(object));
A.setArriveTime(0);
A.setServiceTime(4);
B.setArriveTime(1);
B.setServiceTime(3);
C.setArriveTime(2);
C.setServiceTime(5);
D.setArriveTime(3);
D.setServiceTime(2);
E.setArriveTime(4);
E.setServiceTime(4);
System.out.println();
System.out.println("--------现在正在使用的是SJF调度算法-------------");
// 声明两个DispatchAlgorithm变量,存放doJudgeBeforeSJF返回的下一个执行的值
DispatchAlgorithm lastObject = new DispatchAlgorithm();
DispatchAlgorithm nextObject = A;
defaultObject.doSJP(lastObject, nextObject);// 第一次调度
lastObject = nextObject;
nextObject = defaultObject.doJudgeBeforSJP(lastObject, object);
defaultObject.doSJP(lastObject, nextObject);//
第二次调度
lastObject = nextObject;
nextObject = defaultObject.doJudgeBeforSJP(lastObject, object);
defaultObject.doSJP(lastObject, nextObject);// 第三次调度
lastObject = nextObject;
nextObject = defaultObject.doJudgeBeforSJP(lastObject, object);
defaultObject.doSJP(lastObject, nextObject);// 第四次调度
lastObject = nextObject;
nextObject = defaultObject.doJudgeBeforSJP(lastObject, object);
defaultObject.doSJP(lastObject, nextObject);// 第五次调度
System.out.print("到达时间 ");
defaultObject.printArriveTime(object);
System.out.println();
System.out.print("服务时间 ");
defaultObject.printServiceTime(object);
System.out.println();
System.out.print("完成时间 ");
defaultObject.printFinishTime(object);
System.out.println();
System.out.print("周转时间 ");
defaultObject.printRoundTime(object);
System.out.println();
System.out.print("带权周转时间 ");
defaultObject.printWithWeightTime(object);
System.out.println();
System.out.print("平均周转时间 "+defaultObject.printAverageRoundTime(object));
System.out.println();
System.out.print("平均带权周转时间 "+defaultObject.printAverageWithWeightTime(object));[img][/img]
}
}
[img][/img]