Skip to content

Instantly share code, notes, and snippets.

@alfredrichards
Created November 18, 2020 11:49
Show Gist options
  • Save alfredrichards/fe3f50aa8ab79fb2a501293a1b9e76e1 to your computer and use it in GitHub Desktop.
Save alfredrichards/fe3f50aa8ab79fb2a501293a1b9e76e1 to your computer and use it in GitHub Desktop.
Java CompletableFuture example
Supplier<List<Integer>> supplier = () -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return Arrays.asList(1, 2, 3);
};
Consumer<List<Integer>> consumer = input -> {
int sum = input.stream().reduce(0, Integer::sum);
System.out.println("The sum is: " + sum);
};
CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(supplier)
.thenAccept(consumer);
completableFuture.join();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment