[JAVA-13963] Align code with article + clean up (#13013)
Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
parent
748ccaef61
commit
b8b8ed889b
|
@ -1,8 +1,6 @@
|
||||||
package com.baeldung.completablefuture;
|
package com.baeldung.concurrent.completablefuture;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.util.concurrent.CancellationException;
|
import java.util.concurrent.CancellationException;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
@ -17,8 +15,6 @@ import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class CompletableFutureLongRunningUnitTest {
|
public class CompletableFutureLongRunningUnitTest {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(CompletableFutureLongRunningUnitTest.class);
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenRunningCompletableFutureAsynchronously_thenGetMethodWaitsForResult() throws InterruptedException, ExecutionException {
|
public void whenRunningCompletableFutureAsynchronously_thenGetMethodWaitsForResult() throws InterruptedException, ExecutionException {
|
||||||
Future<String> completableFuture = calculateAsync();
|
Future<String> completableFuture = calculateAsync();
|
||||||
|
@ -78,7 +74,7 @@ public class CompletableFutureLongRunningUnitTest {
|
||||||
public void whenAddingThenAcceptToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
|
public void whenAddingThenAcceptToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
|
||||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
|
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
|
||||||
|
|
||||||
CompletableFuture<Void> future = completableFuture.thenAccept(s -> LOG.debug("Computation returned: " + s));
|
CompletableFuture<Void> future = completableFuture.thenAccept(s -> System.out.println("Computation returned: " + s));
|
||||||
|
|
||||||
future.get();
|
future.get();
|
||||||
}
|
}
|
||||||
|
@ -87,7 +83,7 @@ public class CompletableFutureLongRunningUnitTest {
|
||||||
public void whenAddingThenRunToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
|
public void whenAddingThenRunToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException {
|
||||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
|
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
|
||||||
|
|
||||||
CompletableFuture<Void> future = completableFuture.thenRun(() -> LOG.debug("Computation finished."));
|
CompletableFuture<Void> future = completableFuture.thenRun(() -> System.out.println("Computation finished."));
|
||||||
|
|
||||||
future.get();
|
future.get();
|
||||||
}
|
}
|
||||||
|
@ -120,7 +116,7 @@ public class CompletableFutureLongRunningUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingThenAcceptBoth_thenWaitForExecutionOfBothFutures() throws ExecutionException, InterruptedException {
|
public void whenUsingThenAcceptBoth_thenWaitForExecutionOfBothFutures() throws ExecutionException, InterruptedException {
|
||||||
CompletableFuture.supplyAsync(() -> "Hello")
|
CompletableFuture.supplyAsync(() -> "Hello")
|
||||||
.thenAcceptBoth(CompletableFuture.supplyAsync(() -> " World"), (s1, s2) -> LOG.debug(s1 + s2));
|
.thenAcceptBoth(CompletableFuture.supplyAsync(() -> " World"), (s1, s2) -> System.out.println(s1 + s2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
Loading…
Reference in New Issue