Which executor is used when composing Java CompletableFutures?

前端 未结 2 1318
忘掉有多难
忘掉有多难 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:33

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

提交回复
热议问题