Which executor is used when composing Java CompletableFutures?

空扰寡人 提交于 2019-12-03 15:20:45

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.

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!