📄 simulation.java
字号:
package com.persia.simulation;
import java.util.LinkedList;
import java.util.Random;
public class Simulation {
private Random ran=new Random();
private LinkedList<Task> queue=new LinkedList<Task>();
private int taskSize=0;
private int TerminalSize=0;
private double[] events=new double[1000];
private final double INFINITE=Math.pow(10.0,10);
private int targetTaskSize=0;
private Task task=new Task();
private boolean statusBusy=false;
private double cycleTime=0.1;
private double currentTime=0.0;
private int numberOfTaskInSystem=0;
private int finishNumer=0;
private double stayingTime=0.0;
private double theta1=25;
private double theta2=0.8;
public int getTerminalSize() {
return TerminalSize;
}
public void setTerminalSize(int terminalSize) {
TerminalSize = terminalSize;
}
public Simulation(int terminalSize,int targetTaskSize){
this.TerminalSize=terminalSize;
this.targetTaskSize=targetTaskSize;
}
public Simulation(){
}
public void initSimulation(){
for(int i=1;i<TerminalSize+1;i++){
events[i]=this.GenerateExpRandom(theta1);
}
events[TerminalSize+1]=this.INFINITE;
}
public int minEvents(){
double min=this.INFINITE;
int location=-1;
for(int i=1;i<=TerminalSize+1;i++){
if(events[i]<min){
min=events[i];
location=i;
}
}
return location;
}
public void arrive(int terminalNo){
this.currentTime=events[terminalNo];
this.numberOfTaskInSystem++;
Task tmp=new Task();
tmp.setArriveTime(events[terminalNo]);
tmp.setServiceTime(this.GenerateExpRandom(theta2));
tmp.setTerminalNo(terminalNo);
queue.add(tmp);
System.out.println("at "+this.currentTime+"----terminal["+tmp.getTerminalNo()+"] arrived...");
events[terminalNo]=this.INFINITE;
if(statusBusy==false){
this.statusBusy=true;
this.task=this.queue.getFirst();
this.occupyCPU();
}
}
public void occupyCPU(){
//this.task=queue.getFirst();
if(task.getServiceTime()<=this.cycleTime){
events[TerminalSize+1]=task.getServiceTime()+this.currentTime;
task.setServiceTime(0.0);
}else{
events[TerminalSize+1]=this.cycleTime+this.currentTime;
task.setServiceTime(task.getServiceTime()-this.cycleTime);
}
}
public void depart(){
this.currentTime=events[TerminalSize+1];
if(task.getServiceTime()==0){
// 可以离开系统,计算停留时间
this.stayingTime+=this.currentTime-task.getArriveTime();
this.finishNumer++;
//安排此终端下次到达时间
events[task.getTerminalNo()]=this.currentTime+this.GenerateExpRandom(theta1);
this.numberOfTaskInSystem--;
queue.removeFirst();
System.out.println("at "+this.currentTime+"----terminal["+task.getTerminalNo()+"] depart cpu...");
// System.out.println("这个终端下次到达的时间:"+events[task.getTerminalNo()]);
// System.out.println("这个终端前一个终端到达的时间:"+events[task.getTerminalNo()-1]);
//System.out.println("这个终端后一个终端到达的时间:"+events[task.getTerminalNo()+1]);
if(this.numberOfTaskInSystem==0){
this.statusBusy=false;
events[TerminalSize+1]=this.INFINITE;
}else{
this.task=queue.getFirst();
this.occupyCPU();
}
}else{
if(this.numberOfTaskInSystem==1){
this.occupyCPU();
}else{
Task tmp=queue.getFirst();
queue.removeFirst();
queue.addLast(tmp);
this.task=queue.getFirst();
this.occupyCPU();
}
}
}
public double startSimulation(){
this.initSimulation();
while(this.taskSize<=targetTaskSize){
int next=this.minEvents();
if(next>0 && next<TerminalSize+1){
arrive(next);
this.taskSize++;
}
else
depart();
}
return this.stayingTime/this.finishNumer;
}
public double GenerateExpRandom(double theta){
double random=ran.nextDouble();
return (-1/theta)*Math.log(1-random);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -