简体   繁体   English

并行流和CompletableFuture之间的差异

[英]Difference between parallel stream and CompletableFuture

In the book "Java 8 in action" (by Urma, Fusco and Mycroft) they highlight that parallel streams internally use the common fork join pool and that whilst this can be configured globally, eg using System.setProperty(...), that it is not possibly to specify a value for a single parallel stream. 在“Java 8 in action”(Urma,Fusco和Mycroft)一书中,他们强调并行流在内部使用公共fork连接池,虽然这可以全局配置,例如使用System.setProperty(...),不可能为单个并行流指定值。

I have since seen the workaround that involves running the parallel stream inside a custom made ForkJoinPool. 我已经看到了涉及在自定义ForkJoinPool中运行并行流的解决方法

Later on in the book, they have an entire chapter dedicated to CompletableFuture, during which they have a case study where they compare the respective performance of using a parallelStream VS a CompletableFuture. 在本书的后面,他们有一整章致力于CompletableFuture,在此期间他们有一个案例研究,他们比较使用parallelStream VS和CompletableFuture的各自表现。 It turns out their performance is very similar - they highlight the reason for this as being that they are both as default using the same common pool (and therefore the same amount of threads). 事实证明它们的性能非常相似 - 它们强调了这一点的原因是它们都是默认使用相同的公共池(因此相同数量的线程)。

They go on to show a solution and argue that the CompletableFuture is better in this circumstance as it can be congifured to use a custom Executor, with a thread pool size of the user's choice. 他们继续展示解决方案并争辩说CompletableFuture在这种情况下更好,因为它可以被设置为使用自定义Executor,其用户选择的线程池大小。 When they update the solution to utilise this, the performance is significantly improved. 当他们更新解决方案以利用它时,性能得到显着改善。

This made me think - if one were to do the same for the parallel stream version using the workaround highlighted above, would the performance benefits be similar, and would the two approaches therefore become similar again in terms of performance? 这让我想到 - 如果使用上面突出显示的解决方法为并行流版本做同样的事情,性能优势是否会相似,那么这两种方法在性能方面是否会再次相似? In this case, why would one choose the CompletableFuture over the parallel stream when it clearly takes more work on the developer's part. 在这种情况下,为什么人们会选择CompletableFuture而不是并行流,因为它显然需要开发人员更多的工作。

In this case, why would one choose the CompletableFuture over the parallel stream when it clearly takes more work on the developer's part. 在这种情况下,为什么人们会选择CompletableFuture而不是并行流,因为它显然需要开发人员更多的工作。

IMHO This depends on the interface you are looking to support. 恕我直言这取决于您希望支持的界面。 If you are looking to support an asynchronous API eg 如果您希望支持异步API,例如

CompletableFuture<String> downloadHttp(URL url);

In this case, only a completable future makes sense because you may want to do something else unrelated while you wait for the data to come down. 在这种情况下,只有可完成的未来才有意义,因为您可能希望在等待数据下降时执行其他不相关的操作。

On the other hand parallelStream() is best for CPU bound tasks where you want every tasks to perform a portion of some work. 另一方面, parallelStream()最适合CPU绑定任务,您希望每个任务执行某些工作的一部分。 ie every thread is doing the same thing with different data. 即每个线程使用不同的数据做同样的事情。 As you meantion it is also easier to use. 你的意思是它也更容易使用。

暂无
暂无

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

相关问题 Spring Async、Async with CompletableFuture 和 Java8 并行 stream 之间的区别 - Difference between Spring Async, Async with CompletableFuture and Java8 parallel stream 使用Stream API或CompletableFuture的并行数据库查询? - Parallel db queries using Stream API or CompletableFuture? “CompletionStage”和“CompletableFuture”有什么区别 - What is the difference between 'CompletionStage' and 'CompletableFuture' Java 1.8中并行和顺序流的区别 - The Difference between Parallel and Sequential Stream in terms of Java 1.8 CompletableFuture、Future 和 RxJava 的 Observable 的区别 - Difference between CompletableFuture, Future and RxJava's Observable Java CompletableFuture 的 thenApply 和 thenApplyAsync 有什么区别? - What is the difference between thenApply and thenApplyAsync of Java CompletableFuture? 链接 CompletableFuture 的 stream 还是链接 CompletableFuture? - Chaining stream of CompletableFuture or chaining CompletableFuture? CompletableFuture.thenApplyAsync 和 CompletableFuture.runAsync 与线程守护进程状态下自定义 ExecutorService 的区别 - Difference between CompletableFuture.thenApplyAsync and CompletableFuture.runAsync with custom ExecutorService in thread deamon status Java 8 - 如何使用 CompletableFuture 跟踪异步并行流中调用的异常数 - Java 8 - How to track number of exceptions invoked within an async parallel stream using CompletableFuture Java 8 CompletableFuture,流和超时 - Java 8 CompletableFuture , Stream and Timeouts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM