You're just starting a task only to use it to post a long running task on the UI thread. The Runnable
() -> {
while (true) {
itemData.forEach(data -> {
System.out.println(data.getId());
});
}
}
still runs on the application thread, blocking it.
Only UI updates should be done on the application thread. The heavy work should be done on the other thread.
You should instead post only the updates on the application thread. Something like this:
@Override
protected Void call() throws Exception {
while (true) {
try {
// add a pause between updates
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
Platform.runLater(() -> itemData.forEach(data -> {
System.out.println(data.getId());
}));
}
}
If you post updates too often, this can also make the application unresponsive.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…