CompletableFuture的完成处理程序在哪个线程中执行

CompletableFuture的完成处理程序在哪个线程中执行

本文介绍了CompletableFuture的完成处理程序在哪个线程中执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对CompletableFuture方法有疑问:

I have a question about CompletableFuture method:

public <U> CompletableFuture<U> thenApply(Function<? super T, ? extends U> fn)

事情是JavaDoc说的只是这个:

The thing is the JavaDoc says just this:

线程怎么样?这将在哪个线程中执行?如果未来由线程池完成怎么办?

What about threading? In which thread this is going to be executed? What if the future is completed by a thread pool?

推荐答案

docs可以帮助您更好地理解:

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

使用 ForkJoinPool.commonPool()执行没有显式Executor参数的所有异步方法
(除非它不支持一个
并行度至少为2,在这种情况下,新的Thread是为运行每个任务创建的
。为了简化监视,调试和
跟踪,所有生成的异步任务都是标记
接口的实例 CompletableFuture.AsynchronousCompletionTask

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.

更新 :我也会建议阅读@Mike的,作为对文档细节的进一步有趣分析。

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

这篇关于CompletableFuture的完成处理程序在哪个线程中执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:25