Which executor is used when composing Java CompletableFutures?

前端 未结 2 1146
庸人自扰
庸人自扰 2021-02-10 07:51

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条回答
  •  旧时难觅i
    2021-02-10 08:36

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

提交回复
热议问题