Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Code examples

The thenApply method creates a new stage, that upon completion transforms the result of the single previous stage by the given Function.

CompletionStage<String> stage1 = supplyAsync(() -> sleepAndGet("single"));
CompletionStage<String> stage = stage1.thenApply(
       s -> s.toUpperCase());
assertEquals("SINGLE", stage.toCompletableFuture().get());

The thenCompose method creates a new stage, that upon completion also transforms the result of the single previous stage by the given Function. This method is similar to the thenApply method described above. The difference is that the result of this Function is a subclass of CompletionStage, which is useful when a transformation is a slow operation that is reasonable to execute in a separate stage (possible asynchronously).

CompletionStage<String> stage1 = supplyAsync(() -> sleepAndGet("sequential1"));
CompletionStage<String> stage = stage1.thenCompose(
       s -> supplyAsync(() -> sleepAndGet((s + " " + "sequential2").toUpperCase())));
assertEquals("SEQUENTIAL1 SEQUENTIAL2", stage.toCompletableFuture().get());

The applyToEither method creates a new stage, that upon completion transforms the first result of the previous two stages by the given Function.

CompletionStage<String> stage1 = supplyAsync(() -> sleepAndGet(1, "parallel1"));
CompletionStage<String> stage2 = supplyAsync(() -> sleepAndGet(2, "parallel2"));
CompletionStage<String> stage = stage1.applyToEither(stage2,
       s -> s.toUpperCase());
assertEquals("PARALLEL1", stage.toCompletableFuture().get());

The thenCombine method creates a new stage, that upon completion transforms the two results of the previous two stages by the given BiFunction.

CompletionStage<String> stage1 = supplyAsync(() -> sleepAndGet("parallel1"));
CompletionStage<String> stage2 = supplyAsync(() -> sleepAndGet("parallel2"));
CompletionStage<String> stage = stage1.thenCombine(stage2,
       (s1, s2) -> (s1 + " " + s2).toUpperCase());
assertEquals("PARALLEL1 PARALLEL2", stage.toCompletableFuture().get());

The thenAccept method creates a new stage, that upon completion consumes the single previous stage by the given _ Consumer_.

CompletableFuture<String> stage1 = supplyAsync(() -> sleepAndGet("single"));
CompletionStage<Void> stage = stage1.thenAccept(
       s -> logger.info("consumes the single: {}", s));
assertNull(stage.toCompletableFuture().get());

The acceptEither method creates a new stage, that upon completion consumes the first result of the previous two stages by the given Consumer.

CompletionStage<String> stage1 = supplyAsync(() -> sleepAndGet(1, "parallel1"));
CompletionStage<String> stage2 = supplyAsync(() -> sleepAndGet(2, "parallel2"));
CompletionStage<Void> stage = stage1.acceptEither(stage2,
       s -> logger.info("consumes the first: {}", s));
assertNull(stage.toCompletableFuture().get());

The thenAcceptBoth method creates a new stage, that upon completion consumes the two results of the previous two stages by the given BiConsumer.

CompletionStage<String> stage1 = supplyAsync(() -> sleepAndGet(1, "parallel1"));
CompletionStage<String> stage2 = supplyAsync(() -> sleepAndGet(2, "parallel2"));
CompletionStage<Void> stage = stage1.thenAcceptBoth(stage2,
       (s1, s2) -> logger.info("consumes both: {} {}", s1, s2));
assertNull(stage.toCompletableFuture().get());

The thenRun method creates a new stage, that upon completion of the single previous stage runs the given Runnable.

CompletionStage<String> stage1 = supplyAsync(() -> sleepAndGet("single"));
CompletionStage<Void> stage = stage1.thenRun(
       () -> logger.info("runs after the single"));
assertNull(stage.toCompletableFuture().get());

The runAfterEither method creates a new stage, that upon completion of the first of the previous two stages, runs the given Runnable.

CompletionStage<String> stage1 = supplyAsync(() -> sleepAndGet(1, "parallel1"));
CompletionStage<String> stage2 = supplyAsync(() -> sleepAndGet(2, "parallel2"));
CompletionStage<Void> stage = stage1.runAfterEither(stage2,
       () -> logger.info("runs after the first"));
assertNull(stage.toCompletableFuture().get());

The runAfterBoth method creates a new stage, that upon completion of the previous two stages, runs the given _ Runnable_.

CompletionStage<String> stage1 = supplyAsync(() -> sleepAndGet(1, "parallel1"));
CompletionStage<String> stage2 = supplyAsync(() -> sleepAndGet(2, "parallel2"));
CompletionStage<Void> stage = stage1.runAfterBoth(stage2,
       () -> logger.info("runs after both"));
assertNull(stage.toCompletableFuture().get());