Questions tagged [completable-future]

In Java 8, a Future that may be explicitly completed (setting its value and status), and may include dependent functions and actions that trigger upon its completion.

Introduced in Java 8, CompletableFuture is a Future that may be explicitly completed (setting its value and status), and may include dependent functions and actions that trigger upon its completion.

When two or more threads attempt to complete, completeExceptionally, or cancel a CompletableFuture, only one of them succeeds. Oracle Documentation

1386 questions
186
votes
9 answers

CompletableFuture | thenApply vs thenCompose

I can't get my head around the difference between thenApply and thenCompose. So, could someone provide a valid use case? From the Java docs: thenApply(Function fn) Returns a new CompletionStage that, when this stage…
GuyT
  • 4,316
  • 2
  • 16
  • 30
182
votes
2 answers

CompletableFuture class: join() vs get()

What is the difference between the get() and join() methods of the CompletableFuture class? Below is the my code: List process() { List messages = Arrays.asList("Msg1", "Msg2", "Msg3", "Msg4", "Msg5", "Msg6", "Msg7", "Msg8",…
Nomeswaran
  • 1,831
  • 2
  • 10
  • 7
101
votes
4 answers

Throwing exception from CompletableFuture

I have the following code: // How to throw the ServerException? public void myFunc() throws ServerException{ // Some code CompletableFuture a = CompletableFuture.supplyAsync(() -> { try { return someObj.someFunc(); …
ayushgp
  • 4,891
  • 8
  • 40
  • 75
93
votes
9 answers

Convert from List to CompletableFuture

I am trying to convert List> to CompletableFuture>. This is quite useful as when you have many asynchronous tasks and you need to get results of all of them. If any of them fails then the final future fails. This is how…
Jatin
  • 31,116
  • 15
  • 98
  • 163
92
votes
1 answer

Java 8 CompletableFuture.allOf(...) with Collection or List

Java 8 has a function CompletableFuture.allOf(CompletableFuture...cfs) that returns a CompletableFuture that is completed when all the given futures complete. However, I almost always am not dealing with an array of CompletableFutures, but…
therealrootuser
  • 10,215
  • 7
  • 31
  • 46
90
votes
2 answers

Listenablefuture vs Completablefuture

I tried hard but didn't find any article or blog which clearly compares ListenableFuture and CompletableFuture, and provides a good analysis. So if anyone can explain or point me to such a blog or article, it will be really good for me.
Ashutosh Jha
  • 1,465
  • 1
  • 17
  • 28
89
votes
6 answers

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

Suppose I have the following code: CompletableFuture future = CompletableFuture.supplyAsync( () -> 0); thenApply case: future.thenApply( x -> x + 1 ) .thenApply( x -> x + 1 ) .thenAccept( x ->…
Yulin
  • 1,163
  • 1
  • 7
  • 10
69
votes
4 answers

Return CompletableFuture or CompletableFuture?

I want to write an asynchronous method that returns a CompletableFuture. The only purpose of the future is to track when the method is complete, not its result. Would it be better to return CompletableFuture or CompletableFuture? Is there a…
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
68
votes
3 answers

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

I am using CompletableFuture as shown below in the code. But concerning the way I should wait till all runnables finish, I found two ways and I do not know the difference between them and which one is the best practice? They are as…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
59
votes
3 answers

What is the difference between 'CompletionStage' and 'CompletableFuture'

I have seen an example in each of them, but I need to know exactly what is the difference in deep because I think I can use both of them to get the same result, So I want to know then I can choose the correct one. What is the benefit of using each…
Ebraheem Alrabeea
  • 2,130
  • 3
  • 23
  • 42
57
votes
4 answers

Difference between thenAccept and thenApply

I'm reading the document on CompletableFuture and The description for thenAccept() is Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied action. and for…
Anqi Lu
  • 671
  • 1
  • 5
  • 5
56
votes
2 answers

CompletableFuture, supplyAsync() and thenApply()

Need to confirm something. The following code: CompletableFuture .supplyAsync(() -> {return doSomethingAndReturnA();}) .thenApply(a -> convertToB(a)); would be the same as: CompletableFuture .supplyAsync(() -> { A a =…
igr
  • 10,199
  • 13
  • 65
  • 111
51
votes
2 answers

Mono vs CompletableFuture

CompletableFuture executes a task on a separate thread ( uses a thread-pool ) and provides a callback function. Let's say I have an API call in a CompletableFuture. Is that an API call blocking? Would the thread be blocked till it does not get a…
XYZ
  • 709
  • 2
  • 9
  • 12
50
votes
5 answers

In which thread do CompletableFuture's completion handlers execute?

I have a question about CompletableFuture method: public CompletableFuture thenApply(Function fn) The thing is the JavaDoc says just this: Returns a new CompletionStage that, when this stage completes normally, is…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
50
votes
2 answers

Surprising behavior of Java 8 CompletableFuture exceptionally method

I have encountered strange behavior of Java 8 CompletableFuture.exceptionally method. If I execute this code, it works fine and prints java.lang.RuntimeException CompletableFuture future = new…
Lukas
  • 13,606
  • 9
  • 31
  • 40
1
2 3
92 93