Skip to content

Instantly share code, notes, and snippets.

@svenreimers
Created November 12, 2013 15:08
Show Gist options
  • Save svenreimers/7432402 to your computer and use it in GitHub Desktop.
Save svenreimers/7432402 to your computer and use it in GitHub Desktop.
Devoxx Hackergarten Lambda Project Use Lambdas to create a method that retrieves values in a thread safe way from the JavaFX scene graph
public static final <T> Future<T> runFuture(Supplier<T> supplier) {
FutureTask<T> futureTask = new FutureTask<>(() -> supplier.get());
Platform.runLater(futureTask);
return futureTask;
}
public static final <T> Optional<T> runFutureValue(Supplier<T> supplier) {
Future<T> runFuture = runFuture(supplier);
try {
return Optional.of(runFuture.get());
} catch (InterruptedException iex) {
Thread.currentThread().interrupt();
return Optional.empty();
} catch(ExecutionException ex) {
return Optional.empty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment