Skip to content

Instantly share code, notes, and snippets.

@Groostav
Created May 26, 2016 18:55
Show Gist options
  • Save Groostav/4a9252d17700f7aabe0e21da10fb52a8 to your computer and use it in GitHub Desktop.
Save Groostav/4a9252d17700f7aabe0e21da10fb52a8 to your computer and use it in GitHub Desktop.
empowerops implementation of `runImmediately` for javafx
public class SyncingUtilities {
//...
public static void runImmediatelyOnFXThread(Action actionToRunOnJavaFXThread) {
if (BootstrappingUtilities.isFXApplicationThread()) {
actionToRunOnJavaFXThread.run();
return;
}
Ref<Throwable> exceptionRaisedByJavaFXAction = new Ref<>(null);
CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(() -> {
try {
actionToRunOnJavaFXThread.run();
}
catch (Exception | AssertionError error) {
exceptionRaisedByJavaFXAction.target = error;
}
finally {
latch.countDown();
}
});
try {
latch.await();
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (exceptionRaisedByJavaFXAction.target != null) {
ExceptionUtilities.throwAsUnchecked(exceptionRaisedByJavaFXAction.target);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment