最近写了一段小程序,模拟三个进程在2个CPU核心下的执行情况。
代码如下:
import java.util.*;
public class OperationSystem {
enum OperationType {
// numerical computation
NC(false),
// input output
IO(true);
boolean needBlock;
OperationType(boolean needBlock) {
this.needBlock = needBlock;
}
}
static class Operation {
OperationType type;
int needCpuTime;
public static Operation create(OperationType type, int needCpuTime) {
Operation operation = new Operation();
operation.type = type;
operation.needCpuTime = needCpuTime;
return operation;
}
}
class Process {
String processName;
Queue<Operation> operations;
boolean isRunnable(){
Operation first = operations.peek();
if (first.type == OperationType.IO) {