开发者

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. 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). 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, e.g. plane one, plane three. Th开发者_StackOverflow社区en 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). 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. 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.

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. 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. 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.


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. When a Plane needs a Runway, it can call BlockingQueue.take() to acquire one. When the plane is done with the Runway, it should call BlockingQueue.put() to release it for use by another Plane.

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.

Is there another requirement I'm missing?

Don't understand the purpose of the two ArrayLists?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜