Java CompletableFuture.complete() block
The complete
call triggers all dependent CompletionStage
s.
So if you've previously registered a BiConsumer
with whenComplete
, the complete
will invoke it in its calling thread. In your case, the call to complete
will return when the BiConsumer
you've passed to whenComplete
finishes. This is described in the the class javadoc
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.
(by another caller is the opposite situation, where the thread calling whenComplete
would actually apply the BiConsumer
if the target CompletableFuture
had already been completed.)
Here's a small program to illustrate the behavior:
public static void main(String[] args) throws Exception { CompletableFuture<String> future = new CompletableFuture<String>(); future.whenComplete((r, t) -> { System.out.println("before sleep, executed in thread " + Thread.currentThread()); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("after sleep, executed in thread " + Thread.currentThread()); }); System.out.println(Thread.currentThread()); future.complete("completed"); System.out.println("done");}
This will print
Thread[main,5,main]before sleep, executed in thread Thread[main,5,main]after sleep, executed in thread Thread[main,5,main]done
showing that the BiConsumer
was applied in the main thread, the one that called complete
.
You can use whenCompleteAsync
to force execution of the BiConsumer
in a separate thread.
[...] that executes the given action using this stage's default asynchronous execution facility when this stage completes.
For example,
public static void main(String[] args) throws Exception { CompletableFuture<String> future = new CompletableFuture<String>(); CompletableFuture<?> done = future.whenCompleteAsync((r, t) -> { System.out.println("before sleep, executed in thread " + Thread.currentThread()); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("after sleep, executed in thread " + Thread.currentThread()); }); System.out.println(Thread.currentThread()); future.complete("completed"); System.out.println("done"); done.get();}
will print
Thread[main,5,main]donebefore sleep, executed in thread Thread[ForkJoinPool.commonPool-worker-1,5,main]after sleep, executed in thread Thread[ForkJoinPool.commonPool-worker-1,5,main]
showing that the BiConsumer
was applied in a separate thread.