I want to execute a CompletableFuture
once another CompletableFuture
finishes, regardless of whether or not the first one completes exceptionally (.thenCompose()
only runs when execution completes normally).
For example:
CompletableFuture.supplyAsync(() -> 1L)
.whenComplete((v, e) -> CompletableFuture.runAsync(() -> {
try {
Thread.sleep(1000);
System.out.println("HERE");
} catch(InterruptedException exc) {
return;
}
}))
.whenComplete((v, e) -> System.out.println("ALL DONE"));
This prints
ALL DONE
HERE
and I would like it to be
HERE
ALL DONE
preferably without nesting the second whenComplete()
inside the first.
Note that I don't care about the returned result/exception here.
What is CompletableFuture.completeExceptionally () in Java? completeExceptionally () is an instance method of the CompletableFuture which is used to complete the future with the given exception. The subsequent calls to methods where we can retrieve results like get () and join () throwing the given exception.
thenApply and thenCompose are methods of CompletableFuture. Use them when you intend to do something to CompletableFuture 's result with a Function. thenApply and thenCompose both return a CompletableFuture as their own result.
CompletableFuture<Integer> future = CompletableFuture.supplyAsync ( () -> 1) .thenApply (x -> x+1); thenCompose is used if you have an asynchronous mapping function (i.e. one that returns a CompletableFuture ).
The futureResult is returned without waiting for the long process to complete. Callers who want to get the result of this CompletableFuture can call futureResult.get (). Notice the complete () and completeExceptionally () methods: Both methods help manually complete a Future.
The trick is to use .handle((r, e) -> r)
to suppress the error:
CompletableFuture.runAsync(() -> { throw new RuntimeException(); })
//Suppress error
.handle((r, e) -> r)
.thenCompose((r) ->
CompletableFuture.runAsync(() -> System.out.println("HELLO")));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With