views:

109

answers:

4

Dear all,

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

+3  A: 

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.

Daniel
+1  A: 

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.

Will Hartung
thks for the reply
Haxed
A: 

Why not just have a singleton 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?

tkersh
I don't see why this Tower must be a singleton. The Tower should be reachable to request landing, but you don't need a singleton to achieve this. Moreover, using a singleton will make it harder to properly write tests for the runways.
Scharrels
You have only one Tower, which owns control of both runways. How does that even work with multiple towers? Depending on how complex the model is, you may have Runway objects that hold their own state, but you would still need a single Tower to query the Runways so they don't know about each other. Modeling code close to the real world tends to make it more understandable and reduce complexity.
tkersh
+1  A: 

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.

Tim Bender