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

Listenablefuture vs Completablefuture

Both ListenableFuture and CompletableFuture have an advantage over its parent class Future by allowing the caller to “register” in one way or another a callback to be called when the async action has been completed. With Future you can do this: ExecutorService executor = …; Future f = executor.submit(…); f.get(); f.get() gets blocked until the … Read more

What is the difference between thenApply and thenApplyAsync of Java CompletableFuture?

The difference has to do with the Executor that is responsible for running the code. Each operator on CompletableFuture generally has 3 versions. thenApply(fn) – runs fn on a thread defined by the CompleteableFuture on which it is called, so you generally cannot know where this will be executed. It might immediately execute if the … Read more

Java 8 CompletableFuture.allOf(…) with Collection or List [duplicate]

Unfortunately, to my knowledge CompletableFuture does not support collections. You could do something like this to make the code a bit cleaner, but it essentially does the same thing public <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> futuresList) { CompletableFuture<Void> allFuturesResult = CompletableFuture.allOf(futuresList.toArray(new CompletableFuture[futuresList.size()])); return allFuturesResult.thenApply(v -> futuresList.stream(). map(future -> future.join()). collect(Collectors.<T>toList()) ); } Found this very informative : … Read more

Convert from List to CompletableFuture

Use CompletableFuture.allOf(…): static<T> CompletableFuture<List<T>> sequence(List<CompletableFuture<T>> com) { return CompletableFuture.allOf(com.toArray(new CompletableFuture<?>[0])) .thenApply(v -> com.stream() .map(CompletableFuture::join) .collect(Collectors.toList()) ); } A few comments on your implementation: Your use of .thenComposeAsync, .thenApplyAsync and .thenCombineAsync is likely not doing what you expect. These …Async methods run the function supplied to them in a separate thread. So, in your case, you … Read more

Throwing exception from CompletableFuture

Your code suggests that you are using the result of the asynchronous operation later in the same method, so you’ll have to deal with CompletionException anyway, so one way to deal with it, is public void myFunc() throws ServerException { // Some code CompletableFuture<A> a = CompletableFuture.supplyAsync(() -> { try { return someObj.someFunc(); } catch(ServerException … Read more

CompletableFuture | thenApply vs thenCompose

thenApply is used if you have a synchronous mapping function. 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). It will then return a future with the result directly, rather than a nested future. CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> … Read more