When using JavaFX 8, we need to run interactions with the GUI via Platform.runLater
, or else they will throw exceptions if ran from another thread.
However the implementation of Platform.runLater
never checks whether it currently is on the JavaFX thread.
I wrote the following method:
public static void runSafe(final Runnable runnable) {
Objects.requireNonNull(runnable, "runnable");
if (Platform.isFxApplicationThread()) {
runnable.run();
}
else {
Platform.runLater(runnable);
}
}
This ensures that it can never be run on a non-fx-application thread.
Is there any reason that the default implementation does not do this kind of short-circuiting?