简体   繁体   English

何时使用 CompletableFuture#thenApply(..) 而不是 thenApplyAsync(..)?

[英]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.

相关问题 Java CompletableFuture 的 thenApply 和 thenApplyAsync 有什么区别? - What is the difference between thenApply and thenApplyAsync of Java CompletableFuture? 然后应用于CompletableFuture - thenApply in CompletableFuture CompletableFuture | thenApply 与 thenCompose - CompletableFuture | thenApply vs thenCompose 如果使用thenApply,则CompletableFuture#whenComplete未被调用 - CompletableFuture#whenComplete not called if thenApply is used CompletableFuture-使用thenapply与使用lambda块的优势 - Completablefuture - advantage of using thenapply vs using lambda block CompletableFuture.thenApplyAsync 和 CompletableFuture.runAsync 与线程守护进程状态下自定义 ExecutorService 的区别 - Difference between CompletableFuture.thenApplyAsync and CompletableFuture.runAsync with custom ExecutorService in thread deamon status 何时使用CompletableFuture的非异步方法? - When to use non-async methods of CompletableFuture? 使用Java CompletableFuture方法时的同步行为 - Synchronous behavior when use methods of Java CompletableFuture CompletableFuture.allOf() 在等待完成时是否比 CompletableFuture.join() 循环有任何优势? - Has CompletableFuture.allOf() any advantage over a loop with CompletableFuture.join() when just waiting for completion? 如何强制 CompletableFuture.thenApply() 在运行前一阶段的同一线程上运行? - How to force CompletableFuture.thenApply() to run on the same thread that ran the previous stage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM