如何表示时间的流逝?
How to represent passing of time?
我正在创建一个简单的加油站模拟作业。模拟的总持续时间为一周。根据燃料类型,给汽车加油大约需要 3 分钟。汽车可能会排成一排。现在的问题。我知道如何实现这些方法,但不知道如何在没有像 Thread.sleep()
.
这样的方法的情况下模拟一段时间
P.S。我正在为此任务使用 JavaFX 框架。汽车表示为 javafx.scene.shape.Rectangle
,它们的运动通过 Tranlsate
方法。掌柜也是。
Thread.sleep()
方法接受毫秒值。基本上,您可以 运行 一个更新,然后计算在下一次更新之前您需要休眠多长时间。
您可以使用 System.nanoTime()
测量实时流逝。确保你的 class implements Runnable
。在 运行 方法中,粘贴一个包含 update()
方法的 while 循环来更新汽车。在循环的开始和结束处获得纳秒时间,减去这两个得到经过的时间。从您希望每次更新花费的时间中减去经过的时间,然后让线程休眠。我认为这真的是您所需要的。
代码如下:
public void run() {
int updatesPerSecond = 5;
/* The target time is the time each update should take.
* You want the target time to be in milliseconds.
* so 5 updates a second is 1000/5 milliseconds. */
int targetTime = 1000 / updatesPerSecond;
long currentTime;
long lastTime = System.nanoTime();
long elapsedTime;
long sleepTime;
while (running) {
// get current time (in nanoseconds)
currentTime = System.nanoTime();
// get time elapsed since last update
elapsedTime = currentTime - lastTime;
lastTime = currentTime;
// run your update
update();
// compute the thread sleep time in milliseconds.
// elapsed time is converted to milliseconds.
sleepTime = targetTime - (elapsedTime / 1000000000);
// don't let sleepTime drop below 0
if (sleepTime < 0) {
sleepTime = 1;
}
// attempt to sleep
try {
Thread.sleep(sleepTime);
} catch(Exception e) {
e.printStackTrace();
}
}
}
在 JavaFX 框架中,
你可以简单地使用 PauseTransition to simulate periodic time elapsing. On end of every period update scene graph elements' states. If you are going to change some properties of some node and do various animations you may utilize other types of Transitions. For more fine grained control you can use Timeline with its KeyFrames.
我正在创建一个简单的加油站模拟作业。模拟的总持续时间为一周。根据燃料类型,给汽车加油大约需要 3 分钟。汽车可能会排成一排。现在的问题。我知道如何实现这些方法,但不知道如何在没有像 Thread.sleep()
.
P.S。我正在为此任务使用 JavaFX 框架。汽车表示为 javafx.scene.shape.Rectangle
,它们的运动通过 Tranlsate
方法。掌柜也是。
Thread.sleep()
方法接受毫秒值。基本上,您可以 运行 一个更新,然后计算在下一次更新之前您需要休眠多长时间。
您可以使用 System.nanoTime()
测量实时流逝。确保你的 class implements Runnable
。在 运行 方法中,粘贴一个包含 update()
方法的 while 循环来更新汽车。在循环的开始和结束处获得纳秒时间,减去这两个得到经过的时间。从您希望每次更新花费的时间中减去经过的时间,然后让线程休眠。我认为这真的是您所需要的。
代码如下:
public void run() {
int updatesPerSecond = 5;
/* The target time is the time each update should take.
* You want the target time to be in milliseconds.
* so 5 updates a second is 1000/5 milliseconds. */
int targetTime = 1000 / updatesPerSecond;
long currentTime;
long lastTime = System.nanoTime();
long elapsedTime;
long sleepTime;
while (running) {
// get current time (in nanoseconds)
currentTime = System.nanoTime();
// get time elapsed since last update
elapsedTime = currentTime - lastTime;
lastTime = currentTime;
// run your update
update();
// compute the thread sleep time in milliseconds.
// elapsed time is converted to milliseconds.
sleepTime = targetTime - (elapsedTime / 1000000000);
// don't let sleepTime drop below 0
if (sleepTime < 0) {
sleepTime = 1;
}
// attempt to sleep
try {
Thread.sleep(sleepTime);
} catch(Exception e) {
e.printStackTrace();
}
}
}
在 JavaFX 框架中,
你可以简单地使用 PauseTransition to simulate periodic time elapsing. On end of every period update scene graph elements' states. If you are going to change some properties of some node and do various animations you may utilize other types of Transitions. For more fine grained control you can use Timeline with its KeyFrames.