I have a method on some repository class that returns a CompletableFuture
. The code that completes these futures uses a third party library which blocks. I intend t
Just from my empirical observations while playing around with it, the thread that executes these non-async methods will depend on which happens first, thenCompose
itself or the task behind the Future
.
If thenCompose
completes first (which, in your case, is almost certain), then the method will run on the same thread that is executing the Future
task.
If the task behind your Future
completes first, then the method will run immediately on the calling thread (i.e. no executor at all).
Side Question: If you assigned the intermediate CompletionStage
to a variable and call a method on it, it would get executed on the same thread.
Main Question: Only the first one, so change thenAccept
to thenAcceptAsync
-- all the following ones will execute their steps on the thread that is used for the accept.
Alternative Question: the thread that completed the future from thenCompose
is the same one as was used for the compose.
You should think of the CompletionStages as steps, that are executed in rapid succession on the same thread (by just applying the functions in order), unless you specifically want the step to be executed on a different thread, using async. All next steps are done on that new thread then.
In your current setup the steps would be executed like this:
Thread-1: delete, accept, compose, complete
With the first accept async, it becomes:
Thread-1: delete
Thread-2: accept, compose, complete
As for your last question, about the same thread being used by your callers if they add additional steps -- I don't think there is much you can do about aside from not returning a CompletableFuture
, but a normal Future
.