Skip to content

Instantly share code, notes, and snippets.

@Yatharth0045
Created April 7, 2019 16:20
Show Gist options
  • Save Yatharth0045/75b75ba590035bdab4a541d85b13aa3f to your computer and use it in GitHub Desktop.
Save Yatharth0045/75b75ba590035bdab4a541d85b13aa3f to your computer and use it in GitHub Desktop.
This program is to show the chaining of multiple completablefutures.
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
class Test {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> (int) (Math.random() * 10))
.thenApply(value -> {
System.out.println("Generated random number is " + value);
return value % 2 == 0;
})
.thenAccept(isEven -> System.out.println(isEven ? "Even" : "Odd"))
.thenRun(() -> System.out.println("Computation completed."));
completableFuture.get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment