This is my JavaFX Controller
public class MainController {
private Future<Graph> operation;
private ExecutorService executor = Executors.newSingleThreadExecutor();
@FXML
private void createSession() { //invoked by a button click in the view
//GraphCreationSession implements Callable<Graph>
GraphCreationSession graphSession = new GraphCreationSession();
if (operation != null && !operation.isDone()) {
//cancel previous session
operation.cancel(true);
}
operation = executor.submit(graphSession);
???
}
}
So my question is, what is the idiom to deal with the result of the Future
in a javaFX context?
I know I can do operation.get()
and the thread will block until the operation is finished, but I would be blocking the Application thread. I'm thinking of a callback when the Callable
finishes and I found out CompletableFuture
, which kind of does that via thenAccept but based on this answer the thread will still be blocked, which defeats the point of a Future, like the answer mentions.
In my particular case the result of the Callable (Graph in my sample) contains a result that I want to display in a panel when the operation completes.