In which thread does CompletableFuture's completion handlers execute in?

前端 未结 4 1463
无人及你
无人及你 2020-11-29 04:59

I have a question about CompletableFuture method:

public  CompletableFuture thenApply(Function fn)

4条回答
  •  一向
    一向 (楼主)
    2020-11-29 05:19

    The policies as specified in the CompletableFuture docs could help you understand better:

    • Actions supplied for dependent completions of non-async methods may be performed by the thread that completes the current CompletableFuture, or by any other caller of a completion method.

    • All async methods without an explicit Executor argument are performed using the ForkJoinPool.commonPool() (unless it does not support a parallelism level of at least two, in which case, a new Thread is created to run each task). To simplify monitoring, debugging, and tracking, all generated asynchronous tasks are instances of the marker interface CompletableFuture.AsynchronousCompletionTask.

    Update: I would also advice on reading this answer by @Mike as an interesting analysis further into the details of the documentation.

提交回复
热议问题