Which executor is used when composing Java CompletableFutures?

前端 未结 2 1324
忘掉有多难
忘掉有多难 2021-02-10 08:20

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

2条回答
  •  你的背包
    2021-02-10 08:37

    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.

提交回复
热议问题