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

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

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

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

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

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

What is the recommended way to wait till the Completable future threads finish

If you really want to wait on all futures, you can simply call join() on each of them: growSeedFutureList.forEach(CompletableFuture::join); The main difference compared to using allOf() is that this will throw an exception as soon as it reaches a future completed with an exception, whereas the allOf().join() version will only throw an exception after all … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)