简体   繁体   English

如何同步两个跑道,以便飞机可以使用Java降落?

[英]How to synchronize two runways so planes can land using java?

I seem to have a small problem. 我似乎有一个小问题。 I have an Air Traffic Control Application, with two runways which I am to synchronize in java. 我有一个空中交通管制应用程序,有两个要在Java中同步的跑道 This is to be done because, if there is a plane two that lands while plane one is in the process of landing, it(plane two) does not have to wait but can quickly move to the runway two to land. 这是因为做,如果有一个平面上的两个着落而平面之一是在降落的过程中,它(飞机二)不必等待,但可以快速移动到二号跑道降落。

I have successfully synchronized one runway and I use one ArrayList to store the plane details and the landing works, however landing of plane two will have to wait(about 5 seconds). 我已经成功地同步了一条跑道,并且使用了一个ArrayList来存储飞机的详细信息和着陆工作,但是第二架飞机的着陆将需要等待(大约5秒)。 Any ideas as to, how to synchronize two runways ? 关于如何同步两个跑道的任何想法?

My present idea was to have two ArrayLists (one ArrayList(Even) stores even numbered planes, eg. plane two, plane four) and another ArrayList(Odd) stores odd numbered planes, eg plane one, plane three. 我现在的想法是有两个ArrayLists (一个ArrayList(Even)存储偶数编号的平面,例如平面2,平面4),另一个ArrayList(Odd)存储奇数编号的平面,例如平面1,平面3。 Then I can make ArrayList (Even) to work with runway one and ArrayList (Odd) to work with runway two(using the individual synchronizaion technique I have done for runway one). 然后,我可以使ArrayList (Even)与第一号跑道一起工作,并使ArrayList (Odd)与第二条跑道一起工作(使用我对第一条跑道所做的单独同步技术)。 The Downside is that, if I add 2 odd numbered planes in ArrayList Odd and 20 even numbered planes in ArrayList , when runway two becomes free, it would not be used. 不足之处是,如果我加入2架奇数飞机ArrayList Odd和20架偶数飞机ArrayList ,当二号跑道变成免费的,它不会被使用。 Instead only runway one would be used and the even numbered planes would have to wait. 相反,将仅使用一条跑道,而偶数编号的飞机将不得不等待。

Side note: I do understand that if both runways are occupied, the third plane will have to wait, but this is acceptable according to the markscheme. 旁注:我确实知道,如果两个跑道都被占用,则第三架飞机将不得不等待,但是根据标记方案,这是可以接受的。

Any suggestions ? 有什么建议么 ?

Thank you 谢谢

To do this properly you need to have only one queue that you put the incoming aircraft into. 要正确执行此操作,您只需要将传入的飞机放入一个队列即可。 Java provides a queue implementation and I suggest you use it rather than rolling your own. Java提供了一个队列实现,我建议您使用它,而不要自己滚动。

Once you have the queue set up you need two runway objects and an 'air traffic controller'. 设置好队列后,您需要两个跑道对象和一个“空中交通管制员”。 The air traffic controller is responsible for checking the runways and if one is available popping a plane off the queue and telling it to land. 空中交通管制员负责检查跑道,是否有空将一架飞机从队列中弹出并告知其降落。

You need a counting semaphore with a value of 2. When someone wishes to land, they consume 1 from the semaphore. 您需要一个值为2的计数信号量。当某人希望着陆时,他们从信号量中消耗1。 When they have the semaphore, they can then choose which runway to land on (simply check the status of the runway and pick a free one). 当他们有信号灯时,他们可以选择降落在哪条跑道上(只需检查跑道的状态并选择一条自由跑道)。

If the semaphore has a 0 value, then folks will have to wait. 如果信号量的值为0,那么人们将不得不等待。 They need not even look at the runways. 他们甚至不需要看跑道。

When a plane leaves the runway, they free up the semaphore. 当飞机离开跑道时,它们释放了信号量。

The Java docs on Semaphore has the details. 有关Semaphore的Java文档具有详细信息。

What you want to do is have each airplane obtain a permit to use a runway when it needs it for landing/taking off. 您要做的是让每架飞机在需要跑道起降时都获得使用跑道的许可。 There are two ways to do this. 有两种方法可以做到这一点。

If knowing the Runway is important: Make the Runway an object and place it into a pool of Runways. 如果了解跑道很重要:将跑道作为对象并将其放入跑道池中。 For instance, place all Runways for an Airport into a BlockingQueue that belongs to the Airport. 例如,将机场的所有跑道放入属于该机场的BlockingQueue When a Plane needs a Runway, it can call BlockingQueue.take() to acquire one. 当飞机需要跑道时,它可以调用BlockingQueue.take()获取一个。 When the plane is done with the Runway, it should call BlockingQueue.put() to release it for use by another Plane. 当飞机在跑道上完成时,它应该调用BlockingQueue.put()释放它以供其他飞机使用。

If you don't care about which Runway: Use a Semaphore to keep track of the number of runways available. 如果您不关心哪个跑道:使用信号量跟踪可用的跑道数量。 Each Plane will need to acquire and release the Semaphore. 每架飞机将需要获取并释放信号量。

Why not just have a single Tower object with methods requestLand() and reportClear(), then just store the runway status as an array inside Tower. 为什么不只使用方法requestLand()和reportClear()拥有一个Tower对象,然后将跑道状态作为数组存储在Tower内部。

Is there another requirement I'm missing? 我还有其他要求吗?

Don't understand the purpose of the two ArrayLists? 不明白两个ArrayList的目的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM