Spring @Async with CompletableFuture

Spring actually does all of the work behind the covers so you don’t have to create the CompletableFuture yourself. Basically, adding the @Async annotation is as if you called your original method (without the annotation) like: CompletableFuture<User> future = CompletableFuture.runAsync(() -> doFoo()); As for your second question, in order to feed it to an executor, … Read more

What is the correct way to create an already-completed CompletableFuture

Since Void can not be instantiated, you can only complete a CompletableFuture<Void> with a null result, which is exactly what you also will get when calling join() on the future returned by allOf() once it has been successfully completed. So you can use CompletableFuture<Void> cf = CompletableFuture.completedFuture(null); to get such an already completed future. But … Read more

What is the difference between ‘CompletionStage’ and ‘CompletableFuture’

CompletionStage<T> is an interface of which CompletableFuture<T> is the only current implementing class. By looking at the javadoc for CompletionStage<T>, you’ll notice it provides methods for taking one CompletionStage<T> and transforming it into another CompletionStage<T>. However, the returned values by the CompletionStage<T> are actually themselves CompletabeFuture<T> objects. So using CompletabeFuture<T> is kind of the same … Read more

What advantage is there to using Spring @Async vs. CompleteableFuture directly?

There is no “vs.” between the two – these are complementary technologies: CompletableFuture provides a convenient way to chain different stages of asynchronous computation – with more flexibility than Spring’s ListenableFuture; @Async provides convenient management of your background tasks and threads, with standard Spring configuration for your executor(s). But both can be combined (since Spring … Read more

ExecutorService.submit(Task) vs CompletableFuture.supplyAsync(Task, Executor)

Besides the return type Future vs. CompletableFuture are there any remarkable differences. Or When to use what? It’s rather simple really. You use the Future when you want the executing thread to wait for async computation response. An example of this is with a parallel merge/sort. Sort left asynchronously, sort right synchronously, wait on left … Read more

Surprising behavior of Java 8 CompletableFuture exceptionally method

This behavior is specified in the class documentation of CompletionStage (fourth bullet): Method handle additionally allows the stage to compute a replacement result that may enable further processing by other dependent stages. In all other cases, if a stage’s computation terminates abruptly with an (unchecked) exception or error, then all dependent stages requiring its completion … Read more

In which thread do CompletableFuture’s completion handlers execute?

As @nullpointer points out, the documentation tells you what you need to know. However, the relevant text is surprisingly vague, and some of the comments (and answers) posted here seem to rely on assumptions that aren’t supported by the documentation. Thus, I think it’s worthwhile to pick it apart. Specifically, we should read this paragraph … Read more

CompletableFuture, supplyAsync() and thenApply()

It is not the same thing. In the second example where thenApply is not used it is certain that the call to convertToB is executed in the same thread as the method doSomethingAndReturnA. But, in the first example when the thenApply method is used other things can happen. First of all, if the CompletableFuture that … Read more

Difference between thenAccept and thenApply

You need to look at the full method signatures: CompletableFuture<Void> thenAccept(Consumer<? super T> action) <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn) thenAccept takes a Consumer and returns a T=Void CF, i.e. one that does not carry a value, only the completion state. thenApply on the other hand takes a Function and returns a CF … Read more