实现步骤如下:
1.实现Process类(状态用Enum标识)
2.构造四个队列(运行队列,阻塞队列,就绪队列,完成队列)
3.初始化时将进程分类放入相应队列中
4.实现改变优先级方法,按优先级排序队列
5.模拟进程处理(策略为时间片不断轮转,发现运行队列中某进程所需时间小于时间片时,将就绪队列(已排序)中的一个进程放入运行队列中,阻塞队列中一个进程放入就绪队列中。下次时间片轮转的时候运行队列必定有一个进程进入完成队列,基本保持队列的稳定。当阻塞队列中的进程处理完时,进程模拟结束)
6.进行部分方法的单元测试
测试数据如下:
2
8
A 3 2 1 0 RUNNING
B 6 5 2 1 READY
C 1 8 5 2 WAITING
D 9 6 8 4 RUNNING
E 1 9 3 1 READY
F 7 7 6 2 WAITING
G 8 15 10 0 WAITING
H 7 5 11 0 READY
运行结果如下:
测试结果如下:
写一个进程类
package com.process.test;
public class Process {
private int needTime;
private int priority;
private int arriveTime;
private int usedTime;
private String name;
private State state;
/**
* Initial Process
* @param name Process name
* @param priority Process priority
* @param needTime Process needTime
* @param arriveTime Process arriveTime
* @param usedTime Process usedTime
* @param state Process state
*/
public Process(String name, int priority, int needTime, int arriveTime, int usedTime, State state) {
this.name = name;
this.arriveTime = arriveTime;
this.needTime = needTime;
this.priority = priority;
this.usedTime = usedTime;
this.state = state;
}
public int getArriveTime() {
return arriveTime;
}
public int getNeedTime() {
return needTime;
}
public int getPriority() {
return priority;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setNeedTime(int needTime) {
this.needTime = needTime;
}
public void setPriority(int priority) {
this.priority = priority;
}
public int getUsedTime() {
return usedTime;
}
public void setUsedTime(int usedTime) {
this.usedTime = usedTime;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
}
进程状态用Enum标识
package com.process.test;
public enum State {
FINISHED, RUNNING, WAITING, READY;
}
模拟进程处理类
package com.process.test;
import com.process.test.Process;
import com.process.test.State;
import java.util.*;
public class SimulateDealProcess {
static int pTime;
static List<Process> readyQueue;
static List<Process> waitingQueue;
static List<Process> queue;
static List<Process> finishedQueue;
static final String READY = "READY", WAITING = "WAITING", FINISHED = "FINISHED", RUNNING = "RUNNING";
static Comparator<Process> comparator = new Comparator<Process>() {
@Override
public int compare(Process o1, Process o2) {
if (o1.getPriority() >= o2.getPriority()) {
return -1;
} else if (o1.getPriority() < o2.getPriority()) {
return 1;
} else {
if (o1.getArriveTime() < o2.getArriveTime()) {
return -1;
} else {
return 1;
}
}
}
};
public static void main(String[] args) {
queue = new LinkedList<>();
readyQueue = new LinkedList<>();
waitingQueue = new LinkedList<>();
finishedQueue = new LinkedList<>();
int count;
int needTime;
int priority;
int arriveTime;
int usedTime;
String name;
Process process;
String processState;
State state;
Scanner sca = new Scanner(System.in);
System.out.println("Please input time piece(Integer)");
pTime = sca.nextInt();
while (pTime <= 0) {
System.out.println("Please input valid time piece!");
pTime = sca.nextInt();
}
System.out.println("Please input number of processes");
count = sca.nextInt();
while (count <= 0) {
System.out.println("Please input valid number of processes");
count = sca.nextInt();
}
System.out.println("Please input process name, priority, needTime, arrivalTime, usedTime, state:");
for (int i = 0; i < count; i++) {
name = sca.next();
priority = sca.nextInt();
needTime = sca.nextInt();
arriveTime = sca.nextInt();
usedTime = sca.nextInt();
processState = sca.next();
state = State.valueOf(processState);
process = new Process(name, priority, needTime, arriveTime, usedTime, state);
switch (state) {
case READY:readyQueue.add(process);break;
case RUNNING:queue.add(process);break;
case FINISHED:finishedQueue.add(process);break;
case WAITING:waitingQueue.add(process);break;
default:
System.out.println("Invalid state, state can only be :FINISHED, RUNNING, WAITING and READY;");
}
}
sortQueue(queue);
sortQueue(finishedQueue);
sortQueue(readyQueue);
sortQueue(waitingQueue);
//choice flag
int flag;
//break while sFlag
boolean sFlag = true;
//choose queue
String chooseQueue;
// index of queue
int index;
int priotity;
while (sFlag) {
System.out.println("1.View ReadyQueue State 2.View RunningQueue State" +
" 3.View WaitingQueue State\t" +
"4.View FinishedQueue State\n5.Process processing Simulation" +
" 6.Change process priority 7.exit");
flag = sca.nextInt();
switch (flag) {
case 1:print(readyQueue);break;
case 2:print(queue);break;
case 3:print(waitingQueue);break;
case 4:print(finishedQueue);break;
case 5:dealProcess();break;
case 6:
System.out.println("Please input queue name(READY, WAITING, RUNNING, FINISHED), " +
"position, priority:");
chooseQueue = sca.next();index = sca.nextInt();priority = sca.nextInt();
changePriority(chooseQueue, index, priority);break;
case 7:sFlag = false;break;
default:
System.out.println("Command Invalid");
}
}
}
public static void sortQueue(List<Process> processList) {
Collections.sort(processList, comparator);
}
public static boolean judgeRange(List<Process> queue, int index) {
return (index <= queue.size()) && (index > 0);
}
public static void changePriority(String strQueue, int index, int priority) {
Process process;
List<Process> tempQueue = null;
switch (strQueue) {
case READY:tempQueue = readyQueue;break;
case WAITING:tempQueue = waitingQueue;break;
case RUNNING:tempQueue = queue;break;
case FINISHED:tempQueue = finishedQueue;break;
default:break;
}
if (tempQueue == null) {
System.out.println("have no " + strQueue + " queue");
} else if ((judgeRange(tempQueue, index))) {
process = tempQueue.get(index-1);
process.setPriority(priority);
} else {
System.out.println("Range Invalid");
}
}
/*
A 3 2 1 0 RUNNING
B 6 5 2 1 READY
C 1 8 5 2 WAITING
D 9 6 8 4 RUNNING
E 1 9 3 1 READY
F 7 7 6 2 WAITING
G 8 15 10 0 WAITING
H 7 5 11 0 READY
*/
public static void dealProcess() {
Process process, waitingProcess, readyProcess;
Iterator<Process> readyIterator, waitingIterator, iterator;
boolean endFlag = true;
while (endFlag) {
iterator = queue.iterator();
readyIterator = readyQueue.iterator();
waitingIterator = waitingQueue.iterator();
while (iterator.hasNext()) {
process = iterator.next();
try {
Thread.sleep(1000 * pTime);
if (process.getNeedTime() - pTime <= 0) {
process.setState(State.FINISHED);
process.setUsedTime(process.getUsedTime() + process.getNeedTime());
process.setNeedTime(0);
finishedQueue.add(process);
iterator.remove();
System.out.println("Process" + process.getName() + " have been finished!");
print(finishedQueue);
} else {
process.setUsedTime(process.getUsedTime() + pTime);
process.setNeedTime(process.getNeedTime() - pTime);
print(queue);
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (readyIterator.hasNext()) {
readyProcess = readyIterator.next();
readyProcess.setState(State.RUNNING);
queue.add(readyProcess);
readyIterator.remove();
System.out.println("Process " + readyProcess.getName() + " come in RunningQueue!");
if (waitingIterator.hasNext()) {
waitingProcess = waitingIterator.next();
waitingProcess.setState(State.READY);
readyQueue.add(waitingProcess);
System.out.println("Process " + waitingProcess.getName() + " come in ReadyQueue!");
waitingIterator.remove();
}
sortQueue(queue);
} else if (queue.isEmpty()) {
endFlag = false;
}
}
}
public static void print(List<Process> queue) {
System.out.println("Name \tPriority \t\tNeedTime \t\tArrivalTime \t\tUsedTime \t\tProcessState");
for (Process p : queue) {
System.out.printf("%-5s %3d \t\t\t%02ds \t\t%02ds " +
"\t\t\t%02ds \t\t%s\n\n", p.getName(), p.getPriority(), p.getNeedTime(),
p.getArriveTime(), p.getUsedTime(), p.getState());
}
}
//Test print method
public static String processInformation(List<Process> queue, int index) {
Process process = queue.get(index);
return process.getName() + " " + process.getPriority() + " " +
process.getNeedTime() + " " + process.getArriveTime() + " " + process.getUsedTime()
+ " " + process.getState();
}
}
单元测试类
package test;
import javafx.concurrent.Worker;
import org.junit.Assert;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import com.process.test.Process;
import com.process.test.State;
import com.process.test.SimulateDealProcess;
import java.util.LinkedList;
import java.util.List;
/**
* com.process.test.SimulateDealProcess Tester.
*
* @author <Authors name>
* @version 1.0
*/
public class SimulateDealProcessTest {
LinkedList<Process> processes;
@Before
public void before() throws Exception {
processes = new LinkedList<Process>();
}
/**
*
* Method: sortQueue(List<com.process.test.Process> processList)
*
*/
@Test
public void testSortQueue() throws Exception {
//TODO: Test goes here...
processes.add(new Process("A", 4,3, 3, 0, State.RUNNING));
processes.add(new Process("B", 4, 6, 1, 1, State.RUNNING));
processes.add(new Process("C", 7, 10, 4, 2, State.RUNNING));
SimulateDealProcess.sortQueue(processes);
Assert.assertEquals("C", processes.get(0).getName());
Assert.assertEquals("B", processes.get(1).getName());
Assert.assertEquals("A", processes.get(2).getName());
}
/**
*
* Method: judgeRange(List<com.process.test.Process> queue, int index)
*
*/
@Test
public void testJudgeRange() throws Exception {
//TODO: Test goes here...
processes.add(new Process("A", 4,3, 3, 0, State.RUNNING));
processes.add(new Process("B", 4, 6, 1, 1, State.RUNNING));
processes.add(new Process("C", 7, 10, 4, 2, State.RUNNING));
processes.add(new Process("E", 9, 5, 4, 2, State.RUNNING));
Assert.assertFalse(SimulateDealProcess.judgeRange(processes, 5));
Assert.assertFalse(SimulateDealProcess.judgeRange(processes, -2));
Assert.assertFalse(SimulateDealProcess.judgeRange(processes, 0));
Assert.assertTrue(SimulateDealProcess.judgeRange(processes, processes.size() - 1));
Assert.assertTrue(SimulateDealProcess.judgeRange(processes, processes.size()));
}
/**
*
* Method: print(List<com.process.test.Process> queue)
*
*/
@Test
public void testPrint() throws Exception {
//TODO: Test goes here...
processes.add(new Process("B", 6,5, 2, 1, State.READY));
Assert.assertEquals("B 6 5 2 1 READY", SimulateDealProcess.processInformation(processes, 0));
}
}