[英]When to use CompletableFuture#thenApply(..) over thenApplyAsync(..)?
In context of CompletableFuture
I understand that thenApply(..)
may use the current thread and may use the a pre-defined executor (eg ForkJoinPool) while thenApplyAsync(..)
ensures that the pre-defined executor will be always used.在
CompletableFuture
上下文中,我理解thenApply(..)
可以使用当前线程并且可以使用预定义的执行器(例如 ForkJoinPool),而thenApplyAsync(..)
确保将始终使用预定义的执行器。
Far as I see the thenApplyAsync(..)
seems be more "reliable" as it never blocks the current thread while thenApply(..)
might be a surprise.就我所见,
thenApplyAsync(..)
似乎更“可靠”,因为它从不阻塞当前线程,而thenApply(..)
可能是一个惊喜。
My question: Which example/scenario would be valid to use thenApply(..)
rather than thenApplyAsync(..)
?我的问题:哪个示例/场景可以使用
thenApply(..)
而不是thenApplyAsync(..)
?
Thanks, Christoph谢谢,克里斯托夫
Yes, thenApplyAsync
would use some excecutor.是的,
thenApplyAsync
会使用一些执行器。 This means that some Runnable object must be created and put in the executor's queue.这意味着必须创建一些 Runnable 对象并将其放入执行程序的队列中。 If the function you want to execute after the complethion of this
CompletableFuture
is very simple, then invoking this method directly may be more efficient than creating envelope Runnable.如果这个
CompletableFuture
后要执行的函数很简单,那么直接调用这个方法可能比创建包络 Runnable 效率更高。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.