Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), e -> System.out.println("Hello, world")));
timeline.setCycleCount(-1);
timeline.play();
new Thread(() -> {
while (true) {
System.out.println("Hello, world!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
在第二个代码片段中,如果我执行 Thread.sleep(some_volatile_variable_of_main_thread_that_changes_overtime)
,我如何使用 Timeline
实现相同的功能。
Timeline
?它是javafx
吗?如果是的话,请考虑将该标签添加到我们的问题中,因为这个类不是标准的java
的一部分(至少从 Java 11 开始)。 - PshemoTimeline
,而它与UI毫无关系。 - Youssef13Timeline
关键帧中的代码保证在FX应用程序线程上执行。时间轴也不会启动新线程,它们只是在现有的FX应用程序线程“脉冲”期间更新。 - James_DJavaFX
程序,使用Timeline
。如果是纯Java,则使用Thread
。如果需要在JavaFX程序中使用线程思想,请查看Service
和/或Task
。 - SedJ601