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
16
votes
2 answers

ForkJoinTask vs CompletableFuture

In Java 8 there are two ways of starting asynchronous computations - CompletableFuture and ForkJoinTask. They both seem fairly similar - the inner classes of CompletableFuture even extend ForkJoinTask. Is there a reason to use one over the…
thecoop
  • 45,220
  • 19
  • 132
  • 189
16
votes
2 answers

How to chose an Executor for CompletableFuture::supplyAsync

CompletableFuture::supplyAsync(() -> IO bound queries) How do I chose an Executor for CompletableFuture::supplyAsync to avoid polluting the ForkJoinPool.commonPool(). There are many options in Executors (newCachedThreadPool, newWorkStealingPool,…
15
votes
1 answer

How does Spring get the result from an endpoint that returns CompletableFuture object?

In the code below, when the endpoint getPerson gets hit, the response will be a JSON of type Person. How does Spring convert CompletableFuture to Person? @RestController public class PersonController { @Autowired private…
MA1
  • 926
  • 10
  • 28
15
votes
1 answer

CompletableFuture allof(..).join() vs CompletableFuture.join()

I am currently using CompletableFuture supplyAsync() method for submitting some tasks to common thread pool. Here is what code snippet looks like: final List>> completableFutures = resolvers.stream() …
Ganesh Shenoy
  • 619
  • 1
  • 10
  • 28
15
votes
2 answers

static ScheduledThreadPoolExecutor in CompletableFuture.Delayer

In java-9 the new method completeOnTimeout in the CompletableFuture class was introduced: public CompletableFuture completeOnTimeout(T value, long timeout, TimeUnit unit) { if (unit == null) …
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
15
votes
2 answers

Spring promoting request scoped bean to child threads (HttpServletRequest)

I tried a lot of things now but i seem to miss a piece of the puzzle. Here is the story: I have a request scoped bean that reads some SessionContext from the HttpServletRequest. This attribute is set in a filter. So this is working absolutely fine…
14
votes
4 answers

How to retain slf4j MDC logging context in CompletableFuture?

When executing async CompletableFuture, the parent threadcontext and moreover the org.slf4j.MDC context is lost. This is bad as I'm using some kind of "fish tagging" to track logs from one request among multiple logfiles. MDC.put("fishid",…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
13
votes
2 answers

What are the ways to pass threadpoolexecutor to CompletableFuture?

I have been working on Java CompletableFuture lately and found , we should always use customized Threadpool. With it, I found two ways of passing threadpool to existing code. Like below This is my ThreadPool in configuration file…
Mayur
  • 864
  • 6
  • 14
  • 25
13
votes
2 answers

Asynchronous non-blocking task with CompletableFutures

I have the need to create an asynchronous, non-blocking task in Java 8, I would like to use CompletableFutures but I'm not sure if it fulfils my needs. To simplify the case, let's say we have an API that retrieve some data for the user but at the…
malavock
  • 341
  • 1
  • 5
  • 21
13
votes
1 answer

When to use non-async methods of CompletableFuture?

I (mostly) understand the three execution methods of CompletableFuture: non-async (synchronous execution) default async (asynchronous using the default executor) custom async (asynchronous using a custom executor) My question is: when should one…
Gili
  • 86,244
  • 97
  • 390
  • 689
13
votes
1 answer

Difference between Java8 thenCompose and thenComposeAsync

Given this piece of code: public List findPrices(String product){ List> priceFutures = shops.stream() .map(shop -> CompletableFuture.supplyAsync( () -> shop.getPrice(product),…
Cilla
  • 419
  • 5
  • 16
13
votes
2 answers

CompletableFuture chaining results

I am trying to chain the calls/results of the methods to the next call. I get compile time error methodE because if am not able to get the reference of objB from the previous call. How can I pass the result of the previous call to the next chain?…
plzdontkillme
  • 1,497
  • 3
  • 20
  • 38
13
votes
1 answer

Difference between parallel stream and CompletableFuture

In the book "Java 8 in action" (by Urma, Fusco and Mycroft) they highlight that parallel streams internally use the common fork join pool and that whilst this can be configured globally, e.g. using System.setProperty(...), that it is not possibly to…
13
votes
2 answers

Should I return CompletableFuture or Future when defining API?

In Java 8, is it better for interface or abstract class to define APIs returning CompletableFuture instead of returning Future? Considering it is ugly converting Future to CompletableFuture and the fact that CompletableFuture will give the caller…
derrdji
  • 12,661
  • 21
  • 68
  • 78
13
votes
3 answers

How do you access completed futures passed to CompletableFuture allOf?

I am trying to get a grip of Java 8 CompletableFuture. How can I join these to person and return them after "allOf". The code under is not working but gives you an idea of what I have tried. In javascript ES6 i would do Promise.all([p1,…
pethel
  • 5,397
  • 12
  • 55
  • 86