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
26
votes
1 answer

CompletableFuture, mutable objects and memory visibility

I'm trying to understand how CompletableFuture in Java 8 interacts with the Java memory model. It seems to me that for programmer sanity, the following should ideally hold true: Actions in the thread that completes a CompletableFuture happen-before…
25
votes
2 answers

Is it correct to convert a CompletableFuture> to a Publisher?

To allow multiple iterations on the resulting stream from a CompletableFuture> I am considering one of the following approaches: Convert the resulting future to CompletableFuture> through: teams.thenApply(st ->…
Miguel Gamboa
  • 8,855
  • 7
  • 47
  • 94
24
votes
3 answers

Why does this CompletableFuture work even when I don't call get() or join()?

I had a question while studying CompletableFuture. The get()/join() methods are blocking calls. What if I don't call either of them? This code calls get(): // Case 1 - Use get() CompletableFuture.runAsync(() -> { try { …
24
votes
2 answers

CompletableFuture already completed with an exception

CompletableFuture.completedFuture() returns a CompletedFuture that is already completed with the given value. How do we construct a CompletableFuture that is already completed exceptionally? Meaning, instead of returning a value I want the future to…
Gili
  • 86,244
  • 97
  • 390
  • 689
24
votes
2 answers

Does an exception handler passed to CompletableFuture.exceptionally() have to return a meaningful value?

I'm used to the ListenableFuture pattern, with onSuccess() and onFailure() callbacks, e.g. ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); ListenableFuture future =…
David Moles
  • 48,006
  • 27
  • 136
  • 235
22
votes
2 answers

Java 11 HTTP client asynchronous execution

I'm trying the new HTTP client API from JDK 11, specifically its asynchronous way of executing requests. But there is something that I'm not sure I understand (sort of an implementation aspect). In the documentation, it says: Asynchronous tasks and…
M A
  • 71,713
  • 13
  • 134
  • 174
22
votes
3 answers

Chaining several CompletionStage only if a condition is achieved

I have several CompletionStage methods that I'd like to chain. The problem is that the result of the first one will determine if the next ones should be executed. Right now the only way to achieve this seems to be passing "special" arguments to next…
Alberto S.
  • 7,409
  • 6
  • 27
  • 46
20
votes
3 answers

CompletableFuture in loop: How to collect all responses and handle errors

I am trying to call a rest api for PUT request in a loop. Each call is a CompletableFuture. Each api call returns an object of type RoomTypes.RoomType I want to collect the responses (both successful and error responses) in different lists. How do…
20
votes
1 answer

Simulate CompletionException in a test

I've got a class HttpClient that has a function that returns CompletableFuture: public class HttpClient { public static CompletableFuture getSize() { CompletableFuture future = ClientHelper.getResults() …
Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
19
votes
1 answer

is using @Async and CompletableFuture in controller can increase performance of our api?

What I am trying to achieve is can I get a better performance by using @Async and CompletableFuture as result in my controller of my RESTApi by using the multi threading in this simple way? here is what I do, here is my controller…
Ke Vin
  • 3,478
  • 11
  • 60
  • 91
19
votes
2 answers

Is it bad practice to use default common fork/join pool with CompletableFuture for doing long blocking calls?

Lets say I have a CompletableFuture which wraps a blocking call like querying a backend using JDBC. In this case, since I am not passing any executor service as a parameter to CompletableFuture.supplyAsync(), the actual blocking work of fetching the…
Chinmay
  • 731
  • 1
  • 8
  • 19
19
votes
4 answers

Java 8 Completable Futures allOf different data types

I have 3 CompletableFutures all 3 returning different data types. I am looking to create a result object that is a composition of the result returned by all the 3 futures. So my current working code looks like this: public ClassD getResultClassD()…
Anand Sunderraman
  • 7,900
  • 31
  • 90
  • 150
19
votes
3 answers

How to use ExecutorService to poll until a result arrives

I have a scenario where I have to poll a remote server checking if a task has completed. Once it has, I make a different call to retrieve the result. I originally figured I should use a SingleThreadScheduledExecutor with scheduleWithFixedDelay for…
kaqqao
  • 12,984
  • 10
  • 64
  • 118
16
votes
2 answers

Is it possible to schedule a CompletableFuture?

Is there any way to schedule CompletableFuture in Java? What I wanted to do is to schedule a task to be executed with some delay, and chain it with other operations to be performed asynchronously when it completes. So far I didn't find any way to do…
16
votes
2 answers

How to create async stacktraces?

UPDATE: The latest version of Intellij IDEA implements exactly what I'm looking for. The question is how to implement this outside of the IDE (so I can to dump async stack traces to log files), ideally without the use of an instrumenting…
Gili
  • 86,244
  • 97
  • 390
  • 689
1 2
3
92 93