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
49
votes
3 answers

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

To run some stuff in parallel or asynchronously I can use either an ExecutorService: Future submit(Runnable task, T result); or the CompletableFuture Api:static CompletableFuture supplyAsync(Supplier supplier, Executor…
dermoritz
  • 12,519
  • 25
  • 97
  • 185
46
votes
2 answers

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

I am using Completable futures in java 8 and I want to write a method that, based on a received parameter, either runs multiple tasks with side effects in parallel and then return their "combined" future (using CompletableFuture.allOf()), or does…
Roy Menczer
  • 500
  • 1
  • 4
  • 11
43
votes
2 answers

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

What's the advantage of using Spring Async vs. Just returning the CompletableFuture on your own?
Christian Bongiorno
  • 5,150
  • 3
  • 38
  • 76
40
votes
1 answer

Spring @Async with CompletableFuture

I have a doubt about this code: @Async public CompletableFuture doFoo() { CompletableFuture fooFuture = new CompletableFuture<>(); try { String fooResult = longOp(); fooFuture.complete(fooResult); }…
Eddy
  • 1,662
  • 2
  • 21
  • 36
37
votes
1 answer

CompletableFuture runAsync vs supplyAsync, when to choose one over the other?

What is the rationale for choosing one over the other? Only difference I could infer after reading the documentation is that runAsync takes Runnable as an input parameter and supplyAsync takes Supplier as an input parameter. This stackoverflow post…
Mr Matrix
  • 1,128
  • 2
  • 14
  • 28
37
votes
3 answers

Thread vs CompletableFuture

What is the advantage of passing code directly to thread vs using CompletableFuture instead? Thread thread = new Thread(() -> {do something}); thread.start(); VS CompletableFuture cf1 = CompletableFuture.runAsync(() -> {do…
Sagar
  • 5,315
  • 6
  • 37
  • 66
36
votes
6 answers

How to cancel Java 8 completable future?

I am playing with Java 8 completable futures. I have the following code: CountDownLatch waitLatch = new CountDownLatch(1); CompletableFuture future = CompletableFuture.runAsync(() -> { try { System.out.println("Wait"); …
Lukas
  • 13,606
  • 9
  • 31
  • 40
34
votes
4 answers

CompletableFuture / ForkJoinPool Set Class Loader

I tackled down a very specific problem, whose solution seems to be something basic: My (Spring) application's classloader hierarchy is something like this: SystemClassLoader -> PlatformClassLoader -> AppClassLoader If I use Java CompleteableFuture…
Maurice Müller
  • 1,312
  • 3
  • 16
  • 32
32
votes
7 answers

How to interrupt underlying execution of CompletableFuture

I know that CompletableFuture design does not control its execution with interruptions, but I suppose some of you might have this problem. CompletableFutures are very good way to compose async execution, but given the case when you want the…
vach
  • 10,571
  • 12
  • 68
  • 106
31
votes
4 answers

ExecutorService vs CompletableFuture

I have been trying to implement an asynchronous process, where the parent method calls a child method which would in-turn call three different methods. I want all of this process to be done asynchronously i.e. after these three calls in the child…
Dwarak
  • 311
  • 1
  • 3
  • 3
30
votes
3 answers

Why does Java have no async/await?

Using async/await it is possible to code asynchronous functions in an imperative style. This can greatly facilitate asynchronous programming. After it was first introduced in C#, it was adopted by many languages such as JavaScript, Python, and…
Stefan Feuerhahn
  • 1,564
  • 1
  • 14
  • 22
30
votes
1 answer

What is CompletableFuture's equivalent of flatMap?

I have this weird type CompletableFuture> but i want CompletableFuture. Is this possible? public Future convert(byte[] htmlBytes) { PhantomPdfMessage htmlMessage = new PhantomPdfMessage(); …
Rico Kahler
  • 17,616
  • 11
  • 59
  • 85
28
votes
2 answers

How to complete a CompletableFuture?

I want a CompletableFuture that only signals the completion (e.g. I don't have a return value). I can instantiate the CompletableFuture as: CompletableFuture future = new CompletableFuture<> (); But what should I feed into the complete…
Xinchao
  • 2,929
  • 1
  • 24
  • 39
27
votes
1 answer

Does CompletionStage always wrap exceptions in CompletionException?

The CompletionStage Javadoc states: [...] if a stage's computation terminates abruptly with an (unchecked) exception or error, then all dependent stages requiring its completion complete exceptionally as well, with a CompletionException holding the…
Gili
  • 86,244
  • 97
  • 390
  • 689
27
votes
5 answers

CompletableFuture: Waiting for first one normally return?

I have some CompletableFutures and I want to run them in parallel, waiting for the first that returns normally. I know I can use CompletableFuture.anyOf to wait for the first to return, but this will return normally or exceptionally. I want to…
Jason
  • 546
  • 1
  • 4
  • 11
1
2
3
92 93