Created
April 26, 2020 13:09
-
-
Save john-nash-rs/d9e3fe96aee7a9dbf83ceed63c256735 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 void demoThenCombine() throws InterruptedException, ExecutionException { | |
long timeStampStart = System.currentTimeMillis(); | |
CompletableFuture<String> completableFutureFirst = CompletableFuture.supplyAsync(() -> { | |
System.out.println("Task Running inside completable Future"); | |
try { | |
Thread.sleep(500); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return "one plus "; | |
}); | |
CompletableFuture<String> completableFutureSecond = CompletableFuture.supplyAsync(() -> { | |
System.out.println("Task Running inside completable Future"); | |
return "two"; | |
}); | |
CompletableFuture<String> completableFutureThird = completableFutureFirst.thenCombine(completableFutureSecond, (resultOne, resultTwo) -> { | |
return (resultOne + resultTwo + " is three"); | |
}); | |
String result = completableFutureThird.get(); | |
long timeStampEnd = System.currentTimeMillis(); | |
System.out.println("Time taken in the Completable Future operation is " + (timeStampEnd - timeStampStart)); | |
System.out.println("Result is :: "+result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment