我对一项有趣的任务感兴趣。我在JavaFx中有UI和另一个更新UI的线程。我从Platform.runLater开始更新。代码:
private void startUpdateDaemon() {
updateUserStatus();
updateTable();
}
private void startUpdateDaemonTask() {
Task task = new Task<Void>() {
@Override
protected Void call() throws Exception {
while (true) {
Platform.runLater(() -> {
startUpdateDaemon();
});
Thread.sleep(1000);
}
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
startUpdateDaemonTask();
}此外,我还可以在另一个类中更新UI:
private void startUpdateDaemonTask() {
Task task = new Task<Void>() {
@Override
protected Void call() throws Exception {
while (true) {
Platform.runLater(new Runnable() {
@Override
public void run() {
updateGameStatus();
}
});
Thread.sleep(1000);
}
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
}最后,我有两个地方叫做"Platform.runLater“,里面有不同的方法。我的问题是,我是否可以用一次调用"Platform.runLater“来创建”一个“方法,并将不同的方法发送给这个方法?我是否可以与消费者一起编写finish方法,并向他发送方法'startUpdateDaemon()‘和'updateGameStatus()'?非常感谢。
发布于 2017-05-29 10:43:45
可以向方法中添加Runnable参数。这个参数给您Platform.runLater
private void startUpdateDaemonTask(Runnable runner) {
Task task = new Task<Void>() {
@Override
protected Void call() throws Exception {
while (true) {
Platform.runLater(runner);
Thread.sleep(1000);
}
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
}现在,您可以使用方法引用来调用此方法:
startUpdateDaemonTask(this::startUpdateDaemon);
startUpdateDaemonTask(this::updateGameStatus);https://stackoverflow.com/questions/44239194
复制相似问题