gpt4 book ai didi

java - 加油站模拟: How to simulate Cars coming at random intervals?

转载 作者:行者123 更新时间:2023-12-01 14:04:59 25 4
gpt4 key购买 nike

任务如下:

The gas station consists of 2 pumps. Each pump has a certain amount of fuel it can distribute. Cars arrive at random intervals and attempt to use one of two pumps:

- If a pump is available and has fuel, the car is immediately allowed to use it. Each car requires a certain amount of fuel (random number), and has to wait for a time proportional to that amount of fuel. For example, a car may need 6 gallons and will use the pump for 3sec, another car may need 10 gallons and will use the pump 5 seconds, etc. When the car is fueled up, it leaves and the another car can use the pump. After fueling a car, the amount of fuel in the pump is adjusted accordingly.
- If both pumps are currently being used, then an arriving car waits until one of the two pumps becomes available.
- If a pump runs out of fuel, it must wait for a tanker to provide more fuel. The tanker arrives periodically (but not too often), and fills both pumps to capacity. While a tanker is servicing the pumps, no cars can use the pumps. forgot to add this

Part I: you must submit a detailed design that matches the above specifications. Your design must use Java threads. You must specify how many and what type of threads you will be using, and how these threads will be synchronized. You can write this stage of the project in pseudo-code. This is to help you understand how the various pieces will together.

Part II: you must submit a complete implementation of your design using Java threads and appropriate synchronization methods. Your implementation must be carefully tested against the above specifications.



我想知道。如何使用 Java 线程来模拟随机进入的汽车?

我非常迷茫,并提前感谢您的帮助。

最佳答案

创建一个 Car 工厂类,该类吐出要添加到 dequeue 的 Car 并随机 hibernate 一段时间。

像所有学生一样,您可能会发现这个问题有点难以解决。如果你不开始把它分解成你可以处理的更小的 block 。考虑一个整体设计并开始实现它的小部分。

例如,这是一个排队论问题。他们可以是汽车、银行排队的人,或者任何与队列交互的东西。不要担心汽车或加油站的细节。这是你的问题:

  • 您有一行在一端添加新条目并在另一端删除它们。它被称为出队数据结构。仔细阅读。看看您是否可以编写(或重用)Java Dequeue 类。完全理解它。编写一个小驱动程序以添加到一端并从另一端删除。
  • 一旦你有了它,你需要编写类来创建新条目以添加到出队和另一个以删除它们。只需编写以固定间隔工作的加法器/去除器即可。您应该看到的是,如果 adder 和 remover 具有完全相同的接口(interface),则排队等待的条目数不会改变。如果加法器比去除器工作得快,则该行应该填满。如果去除器比加法器工作得更快,那么您将永远不会有备份。确保您的简单类表现出这种行为。
  • 添加随机位并开始模拟。您将希望查看行中的条目数如何随时间变化。
  • 现在添加不止一行。您想了解添加更多线条如何改变动态。
  • 关于java - 加油站模拟: How to simulate Cars coming at random intervals?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4043708/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com