我用一条跑道模拟机场。飞机等待起飞和等待起飞的飞机有2个排队。在跑道上一次只能有一架飞机,空中的所有飞机必须在任何飞机起飞前降落。这就是我到目前为止所做的事情(当我确定我不需要它们时,我将会摆脱一些无用的变量):
public class Runway<E> {
private LinkedBlockingQueue<Plane> takeoff;
private LinkedBlockingQueue<Plane> landing;
private LinkedBlockingQueue<Plane> runway;
private int planesLanded;
private int planesTookoff;
private double averageTakeOffWait;
private double averageLandWait;
private int totalTakeoffWait;
private int totalLandingWait;
private int planesWaitingToTakeOff;
private int planesWaitingToLand;
private int maxLandingQueueLength;
private int maxTakeOffQueueLength;
public int timeToLand = 5;
public int timeToTakeoff = 4;
public double landingProbability = .1;
public double takeOffProbability = .1;
public int simulationLength = 1440;
public Runway() {
takeoff = new LinkedBlockingQueue<>();
landing = new LinkedBlockingQueue<>();
runway = new LinkedBlockingQueue<>();
planesLanded = 0;
planesTookoff = 0;
averageTakeOffWait = 0;
averageLandWait = 0;
totalTakeoffWait = 0;
totalLandingWait = 0;
planesWaitingToTakeOff = 0;
planesWaitingToLand = 0;
maxLandingQueueLength = 0;
maxTakeOffQueueLength = 0;
//reset();
}
public void simulate(int duration, double takeoffRate, double landingRate, int landingTime, int takeoffTime) {
for(int count = 0; count < duration; count++) {
if(Math.random() < takeoffRate) {
Plane p = new Plane(landingTime, takeoffTime, count);
takeoff.offer(p);
}
if(Math.random() < landingRate) {
Plane p = new Plane(landingTime, takeoffTime, count);
landing.offer(p);
}
if(runway.size() == 0) {
if(landing.peek() != null) {
Plane landingPlane = landing.poll();
runway.offer(landingPlane);
planesLanded++;
int landTimeWaited = count - landingPlane.getArrivalTime();
totalLandingWait += landTimeWaited;
runway.poll();
} else if(takeoff.peek() != null) {
Plane takeoffPlane = takeoff.poll();
runway.offer(takeoffPlane);
planesTookoff++;
int takeoffTimeWaited = count - takeoffPlane.getArrivalTime();
totalLandingWait += takeoffTimeWaited;
runway.poll();
}
}
}
}
public void report() {
System.out.println(planesLanded + " planes have landed.");
System.out.println(planesTookoff + " planes have taken off.");
System.out.println(landing.size() + " planes still waiting to land.");
System.out.println(takeoff.size() + " planes still waiting to takeoff.");
System.out.println(1.0*totalTakeoffWait/planesTookoff + " average take off wait time.");
System.out.println(1.0*totalLandingWait/planesLanded + " average landing wait time.");
}
}
我的问题是飞机几乎不必等待做任何事情。队列中永远不会留下任何飞机。我知道我必须以某种方式使它成为5&#34;分钟&#34;在下一架飞机做任何事之前通过,但我该怎么做?这是一个试运行:
Runway myAirport = new Runway();
myAirport.simulate(1440, .1, .1, 5, 4);
myAirport.report();
146 planes have landed.
155 planes have taken off.
0 planes still waiting to land.
0 planes still waiting to takeoff.
0.0 average take off wait time.
0.14383561643835616 average landing wait time.
答案 0 :(得分:0)
在循环中,您始终清除跑道队列,这意味着您将始终清除您添加的平面。你应该做什么,是用一些时间来检查更新。
if(runway.size()==0){
//decide to get from takeoff or landing, your current choice is always take off
runway.offer(chosenPlane);
inUseTime = 100;
}else{
Plane p = runway.peek();
if(p.getRunwayTime()>=inUseTime){
runway.poll();
} else{
p.updateRunwayTime();
}
}
在此示例中,如果飞机已完成时间,则该飞机仅从跑道中移除。这意味着在等待当前完成时,更多计划可以进入其他队列。