BAEL-5814 - Is CompletableFuture Non-blocking (#13917)
* BAEL-5814 - Is CompletableFuture Non-blocking * Fix the formatting
This commit is contained in:
parent
23ec3d24d2
commit
d313b76fc7
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.concurrent.completablefuture;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public class NonBlockingExample {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(NonBlockingExample.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
CompletableFuture.supplyAsync(() -> "Baeldung")
|
||||||
|
.thenApply(String::length)
|
||||||
|
.thenAccept(s -> logger.info(String.valueOf(s)));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.concurrent.completablefuture;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
class BlockingUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenCompletableFuture_whenGet_thenReturnResult()
|
||||||
|
throws ExecutionException, InterruptedException {
|
||||||
|
CompletableFuture<String> completableFuture = CompletableFuture
|
||||||
|
.supplyAsync(() -> "Baeldung")
|
||||||
|
.thenApply(String::toUpperCase);
|
||||||
|
|
||||||
|
assertEquals("BAELDUNG", completableFuture.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenCompletableFuture_whenJoin_thenReturnResult() {
|
||||||
|
CompletableFuture<String> completableFuture = CompletableFuture
|
||||||
|
.supplyAsync(() -> "Blocking")
|
||||||
|
.thenApply(s -> s + " Operation")
|
||||||
|
.thenApply(String::toLowerCase);
|
||||||
|
|
||||||
|
assertEquals("blocking operation", completableFuture.join());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user