Skip to content

Instantly share code, notes, and snippets.

@rudrakshbhati
Last active February 20, 2021 18:40
Show Gist options
  • Save rudrakshbhati/6dc241e494ee0974cf815af4737c038b to your computer and use it in GitHub Desktop.
Save rudrakshbhati/6dc241e494ee0974cf815af4737c038b to your computer and use it in GitHub Desktop.
private static CompletableFuture<String> futureCall1(){
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.print("exception occurred");
}
return "Hello";
});
}
private static CompletableFuture<String> futureCall2(){
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(2000);
} catch (Exception e) {
System.out.print("exception occurred");
}
return "World";
});
}
private static void combineFuturesExample(){
CompletableFuture<String> combinedFuture = futureCall1()
.thenCombine(futureCall2(),(string1,string2)-> string1 + " " + string2);
try {
System.out.print(combinedFuture.get());
} catch (Exception e) {
System.out.print("exception occurred");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment