Skip to content

Instantly share code, notes, and snippets.

@Groostav
Last active November 28, 2015 22:17
Show Gist options
  • Save Groostav/2944cb86e87848399924 to your computer and use it in GitHub Desktop.
Save Groostav/2944cb86e87848399924 to your computer and use it in GitHub Desktop.
snippit of our Platform.runImmediately method
public class SyncingUtilities {
//...
public static void runImmediatelyOnFXThread(Action actionToRunOnJavaFXThread, boolean exitOnInterrupt) {
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) {
Log.warning("interrupted");
if(exitOnInterrupt) break; else continue;
}
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