如何使用java同步两条跑道以便飞机着陆?

发布于 2024-09-09 07:33:05 字数 656 浏览 4 评论 0原文

我好像有一个小问题。我有一个空中交通管制应用程序,有两条跑道,我要在java中同步它。这样做是因为,如果飞机二飞机一正在着陆的过程中着陆,则它(飞机二)不必等待,而是可以快速移动到二号跑道降落。

我已经成功同步了一条跑道,并使用一个 ArrayList 来存储飞机详细信息和着陆工作,但是第二架飞机的着陆必须等待(大约 5 秒)。关于如何同步两条跑道有什么想法吗?

我目前的想法是有两个 ArrayList(一个 ArrayList(Even) 存储偶数平面,例如平面二、平面四)和另一个 ArrayList(Odd) 存储奇数编号的平面,例如平面一、平面三。然后我可以使 ArrayList (Even) 与跑道一配合使用,而 ArrayList (Odd) 与跑道二配合使用(使用我为跑道一所做的单独同步技术) 。缺点是,如果我在 ArrayList Odd 中添加 2 架奇数飞机,并在 ArrayList 中添加 20 架偶数飞机,当跑道 2 空闲时,它将不会被使用。相反,只会使用一号跑道,偶数号飞机必须等待。

旁注:我确实理解,如果两条跑道都被占用,第三架飞机将不得不等待,但根据标记方案,这是可以接受的。

有什么建议吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

同展鸳鸯锦 2024-09-16 07:33:05

为了正确地做到这一点,您只需要有一个队列来将进场的飞机放入其中。 Java 提供了一个队列实现,我建议您使用它而不是自己实现。

设置好队列后,您需要两个跑道对象和一个“空中交通管制员”。空中交通管制员负责检查跑道,如果有可用跑道,则将一架飞机从队列中弹出并告诉其着陆。

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.

慕烟庭风 2024-09-16 07:33:05

您需要一个值为 2 的计数信号量。当有人希望着陆时,他们会从信号量中消耗 1。当他们拥有信号量时,他们可以选择降落在哪条跑道上(只需检查跑道的状态并选择一条空闲的跑道)。

如果信号量的值为 0,那么人们将不得不等待。他们甚至不需要看跑道。

当飞机离开跑道时,他们释放信号量。

上的 Java 文档信号量有详细信息。

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.

辞慾 2024-09-16 07:33:05

您想要做的是让每架飞机在需要着陆/起飞时获得使用跑道的许可。有两种方法可以做到这一点。

如果了解跑道很重要:
将跑道作为一个对象并将其放入跑道池中。例如,将机场的所有跑道放入 BlockingQueue 属于机场。当一个 Plane 需要跑道时,它可以调用 BlockingQueue.take() 来获取跑道。当飞机使用完跑道后,它应该调用 BlockingQueue.put() 来释放它以供另一个飞机使用。

如果您不关心哪条跑道:
使用 信号量< /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.

微凉徒眸意 2024-09-16 07:33:05

为什么不只使用一个具有 requestLand() 和 reportClear() 方法的 Tower 对象,然后将跑道状态作为数组存储在 Tower 内。

我还缺少其他要求吗?

不明白这两个ArrayList的用途?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文