最佳答案
我尝试定期在 JavaFX 应用程序后台线程中运行,这会修改一些 GUI 属性。
我想我知道如何使用 javafx.concurrent中的 Task和 Service类,如果不使用 Thread#sleep()方法,我就不知道如何运行这样的周期性任务。如果我可以使用一些从 Executors制造方法(Executors.newSingleThreadScheduledExecutor())的 Executor将是很好的
我尝试每5秒运行一次 Runnable,它会重新启动 javafx.concurrent.Service,但是当调用 service.restart甚至 service.getState()时它会立即挂起。
最后我使用 Executors.newSingleThreadScheduledExecutor(),它每5秒触发一次我的 Runnable,然后 Runnable运行另一个 Runnable,使用:
Platform.runLater(new Runnable() {
//here i can modify GUI properties
}
它看起来非常讨厌: (有没有更好的方法来使用 Task或 Service类?