简体   繁体   English

CompletableFuture supplyAsync(供应商)vs supplyAsync(供应商,执行者)

[英]CompletableFuture supplyAsync(supplier) vs supplyAsync(supplier, executor)

Java Docs says CompletableFuture:supplyAsync(Supplier<U> supplier) runs the task in the ForkJoinPool#commonPool() whereas the CompletableFuture:suppleAsync(supplier, executor) runs it in the given executor. Java Docs说CompletableFuture:supplyAsync(Supplier<U> supplier)ForkJoinPool#commonPool() CompletableFuture:supplyAsync(Supplier<U> supplier)运行任务,而CompletableFuture:suppleAsync(supplier, executor)在给定的执行程序中运行它。

I'm trying to figure out which one to use. 我想弄清楚要使用哪一个。 So my questions are: 所以我的问题是:

  1. What is the ForkJoinPool#commonPool() ? 什么是ForkJoinPool#commonPool()
  2. When should I use supplyAsync(supplier) vs supplyAsync(supplier, executor) ? 我什么时候应该使用supplyAsync(supplier) vs supplyAsync(supplier, executor)

ForkJoinPool#commonPool() is the common pool of threads that Java API provides. ForkJoinPool#commonPool()是Java API提供的公共线程池。 If you ever used stream API , then the parallel operations are also done in this thread pool. 如果您曾使用流API ,那么并行操作也在此线程池中完成。

The advantage of using the common thread pool is that Java API would manage that for you - from creation to destruction. 使用公共线程池的优点是Java API可以为您管理 - 从创建到销毁。 The disadvantage is that you would expect a lot of classes to share the usage of this pool. 缺点是你会期望很多类共享这个池的用法。

If you used an executor, then it is like owning a private pool, so nothing is going to fight with you over the usage. 如果您使用了执行程序,那么它就像拥有一个私人池,因此在使用过程中没有任何东西可以与您抗争。 You make to create the executor yourself, and pass it into CompletableFuture . 您自己创建执行程序,并将其传递给CompletableFuture However, do note that, eventually the actual performance would still depend on what is being done in the threads, and by your hardware. 但是,请注意,最终实际性能仍将取决于线程中的内容以及硬件。

Generally, I find it fine to use the common thread pool for doing more computationally intensive stuff, while executor would be better for doing things that would have to wait for things (like IO). 通常,我发现使用公共线程池来执行更多计算密集型操作是很好的,而执行程序则更适合执行必须等待事物的事情(如IO)。 When you "sleep" in common thread pool thread, it is like using a cubicle in a public washroom to play games on your mobile phone - someone else could be waiting for the cubicle. 当你在普通的线程池线程中“睡觉”时,就像在公共洗手间里使用隔间在你的手机上玩游戏 - 其他人可能正在等待隔间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在CompletableFuture.supplyAsync(Supplier <U>供应商)方法中使用所需数量的工作线程设置ForkJoinPool? - How to set ForkJoinPool with the desired number of worker threads in CompletableFuture.supplyAsync(Supplier<U> supplier) method? 如何为CompletableFuture :: supplyAsync选择Executor - How to chose an Executor for CompletableFuture::supplyAsync ExecutorService.submit(Task)vs CompletableFuture.supplyAsync(Task,Executor) - ExecutorService.submit(Task) vs CompletableFuture.supplyAsync(Task, Executor) CompletableFuture supplyAsync - CompletableFuture supplyAsync CompletableFuture.supplyAsync与新的CompletableFuture() - CompletableFuture.supplyAsync vs new CompletableFuture() CompletableFuture runAsync vs supplyAsync,什么时候选择一个而不是另一个? - CompletableFuture runAsync vs supplyAsync, when to choose one over the other? 使用 Mockito 测试 CompletableFuture.supplyAsync - Testing CompletableFuture.supplyAsync with Mockito ForkJoinPool在CompletableFuture.supplyAsync()中的行为 - Behaviour of ForkJoinPool in CompletableFuture.supplyAsync() CompletableFuture.supplyAsync() 没有 Lambda - CompletableFuture.supplyAsync() without Lambda 获取CompletableFuture.supplyAsync的结果 - Getting the result of a CompletableFuture.supplyAsync
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM