【发布时间】:2019-04-27 09:37:05
【问题描述】:
我在让 ScheduledExecutorService 工作时遇到了一些问题。
这是我正在调用的方法,如果我在没有调度的情况下调用它工作。
但是,如果我通过 ScheduledExecutorService 调用它,我的屏幕不会更新,它也只会运行一次,所以我猜是抛出了异常,但我在服务器或客户端上都没有看到异常。
private void getNextRoundFromServer() throws IOException{
try{
if(!this.connected) return;
this.dos.writeByte(2);
this.dos.flush(); // Send data
this.grid = (Grid) this.ois.readObject(); //i have checked, i do recieve this object
setMainPane(render.render(this.grid)); //this renders the view
} catch(ClassNotFoundException ex){
ex.printStackTrace();
}
}
这就是我的实现方式。
continuousPlayButton.setOnMouseClicked(e -> {
try{
Runnable scheduledRound = () -> {
try {
getNextRoundFromServer();
} catch (IOException ex) {
ex.printStackTrace();
}
};
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(scheduledRound, 1, 5, TimeUnit.SECONDS);
}catch(Exception ex){
ex.printStackTrace();
}
});
为什么我的视图没有更新?
更新
解决方案:
正如用户tjanu 提到的:我忘记在计划任务上调用Platform.runLater。当我在没有安排的情况下调用它时我已经这样做了...... ????
【问题讨论】: