skiwi skiwi - 2 months ago 47
Java Question

Why does Platform.runLater not check if it currently is on the JavaFX thread?

When using JavaFX 8, we need to run interactions with the GUI via

, or else they will throw exceptions if ran from another thread.

However the implementation of
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()) {;
else {

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?


runLater essentially places your Runnable in a queue for execution when the FX thread is available. The method can be accessed through non-FX threads. Imagine that some other threads have placed tasks in the runLater queue, then calling runLater again, whether from the FX thread or not, should place the new task at the tail of that queue.

With the short-circuiting you propose the previous tasks would basically have a lower priority which may not be desirable.