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
String combined = Stream.of(future1, future2, future3) | |
.map(CompletableFuture::join) | |
.collect(Collectors.joining(" ")); | |
assertEquals("Hello Beautiful World", combined); |
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
CompletableFuture<String> future1 | |
= CompletableFuture.supplyAsync(() -> "Hello"); | |
CompletableFuture<String> future2 | |
= CompletableFuture.supplyAsync(() -> "Beautiful"); | |
CompletableFuture<String> future3 | |
= CompletableFuture.supplyAsync(() -> "World"); | |
CompletableFuture<Void> combinedFuture | |
= CompletableFuture.allOf(future1, future2, future3); | |
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
CompletableFuture future = CompletableFuture.supplyAsync(() -> "Hello") | |
.thenAcceptBoth(CompletableFuture.supplyAsync(() -> " World"), | |
(s1, s2) -> System.out.println(s1 + s2)); |
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
CompletableFuture<String> completableFuture | |
= CompletableFuture.supplyAsync(() -> "Hello") | |
.thenCombine(CompletableFuture.supplyAsync( | |
() -> " World"), (s1, s2) -> s1 + s2)); | |
assertEquals("Hello World", completableFuture.get()); |
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
CompletableFuture<String> completableFuture | |
= CompletableFuture.supplyAsync(() -> "Hello") | |
.thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World")); | |
assertEquals("Hello World", completableFuture.get()); |
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
CompletableFuture<String> completableFuture | |
= CompletableFuture.supplyAsync(() -> "Hello"); | |
CompletableFuture<Void> future = completableFuture | |
.thenRun(() -> System.out.println("Computation finished.")); | |
future.get(); |
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
CompletableFuture<String> completableFuture | |
= CompletableFuture.supplyAsync(() -> "Hello"); | |
CompletableFuture<Void> future = completableFuture | |
.thenAccept(s -> System.out.println("Computation returned: " + s)); | |
future.get(); |
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
CompletableFuture<String> completableFuture | |
= CompletableFuture.supplyAsync(() -> "Hello"); | |
CompletableFuture<String> future = completableFuture | |
.thenApply(s -> s + " World"); | |
assertEquals("Hello World", future.get()); |
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
CompletableFuture<String> future | |
= CompletableFuture.supplyAsync(() -> "Hello"); | |
// ... | |
assertEquals("Hello", future.get()); |
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
public Future<String> calculateAsyncWithCancellation() throws InterruptedException { | |
CompletableFuture<String> completableFuture = new CompletableFuture<>(); | |
Executors.newCachedThreadPool().submit(() -> { | |
Thread.sleep(500); | |
completableFuture.cancel(false); | |
return null; | |
}); | |
return completableFuture; |