2

I'm looking JAVA API docs for CompletableFuture, where I've come across with this particular line,

Actions supplied for dependent completions of non-async methods may be performed by the thread that completes the current CompletableFuture, or by any other caller of a completion method.

Refer link description (https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html)

I have confusion for this specific phrase: "or by any other caller of a completion method"

Q1) What does this actually mean? Due to this, I doubt that whenComplete will execute the same thread or there can be chances to be in the different thread?

I've used CompletableFuture with its supplyAsync()/runAsync() and whenComplete() methods.()

CompletableFuture.supplyAsync(
    () -> {DO_SOMETHING}, myExecutor
).whenComplete((result, exception) -> {DO_SOMETHING});

For supplyAsync()/runAsync(), I've used the Executor service. After that, the result of that method (supplyAsync/runAsync) will dive into whenComplete() method.

So the question is: Q2) whenComplete will ALWAYS use the same thread which is used by supplyAsync/runAsync after immediate?

(If whenComplete() method execute in the same thread which is initiated from my Executor at the time of supplyAsync()/runAsync() method.)

Community
  • 1
  • 1
Milan Kamboya
  • 486
  • 2
  • 11
  • I checked these links but still I want to make sure about whenComplete. https://stackoverflow.com/questions/27723546/completablefuture-supplyasync-and-thenapply and https://blog.krecan.net/2013/12/25/completablefutures-why-to-use-async-methods/ – Milan Kamboya Mar 19 '19 at 06:35
  • 1
    specification reads "may", so you cannot assume "always". – Alexei Kaigorodov Mar 19 '19 at 13:07
  • Possible duplicate of [In which thread does CompletableFuture's completion handlers execute in?](https://stackoverflow.com/questions/46060438/in-which-thread-does-completablefutures-completion-handlers-execute-in) – Didier L Mar 21 '19 at 13:37

0 Answers0