Last active
February 20, 2021 18:40
-
-
Save rudrakshbhati/6dc241e494ee0974cf815af4737c038b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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