简体   繁体   English

Task Executor 与 Java 8 并行流

[英]Task Executor vs Java 8 parallel streaming

I can't find a specific answer to the line of investigation that we've been requested to take on对于我们被要求进行的调查,我找不到具体的答案

I see that parallel streams may not be so performant when using small amount of threads, and that apparently it doesn't behave so well when the DB blocks the next request while processing the current one我看到并行流在使用少量线程时可能性能不佳,而且当 DB 在处理当前请求时阻塞下一个请求时,它显然表现不佳

However, I find that the overhead of implementing Task Executor vs Parallel Streams is huge, we've implemented a POC that takes care of our concurrency needs with just this one line of code:但是,我发现实现 Task Executor 与 Parallel Streams 的开销是巨大的,我们已经实现了一个 POC,只需一行代码即可满足我们的并发需求:

List<Map<String, String>> listWithAllMaps = mappedValues.entrySet().parallelStream().map(e -> callPlugins(e))
        .collect(Collectors.toList());

Whereas in Task Executor, we'd need to override the Runnable interface and write some cumbersome code just to get the runnables not to be void and return the values we're reading from the DB, leading us into several hours, if not days of coding, and producing a less maintainable, more bug prone code而在 Task Executor 中,我们需要覆盖 Runnable 接口并编写一些繁琐的代码,只是为了让 runnable 不为空并返回我们从数据库读取的值,这导致我们进入几个小时,如果不是几天编码,并生成更不易维护、更容易出错的代码

However, our CTO is still reluctant to using parallel streams due to unforeseen issues that could come up down the road然而,我们的 CTO 仍然不愿意使用并行流,因为可能会出现不可预见的问题

So the question is, in an environment where I need to make several concurrent read-only queries to a database, using different java-components/REST calls for each query: Is it preferrable in any way to use Task Executor instead of parallel streaming, if so, why?所以问题是,在我需要对数据库进行多个并发只读查询的环境中,对每个查询使用不同的 java 组件/REST 调用:以任何方式使用 Task Executor 而不是并行流,是否更可取,如果是这样,为什么?

Use the TaskExecutor as an Executor for a CompletableFuture .使用TaskExecutor作为CompletableFutureExecutor

List<CompletableFuture> futures = mappedValues.entrySet().stream().map(e - > CompletableFuture.supplyAsync(() -> callPlugins(e), taskExecutor)).collect(Collectors.toList());

List<Map<String, String>> listWithAllMaps = futures.stream().map(CompletableFuture::join).collect(Collectors.toList());

Not sure how this is cumbersome.不知道这有多麻烦。 Yes it is a bit more code, but with the advantage that you can easily configure the TaskExecutor and increase the number of threads, queueu-size etc. etc.是的,这是更多的代码,但优点是您可以轻松配置TaskExecutor并增加线程数、队列大小等。

DISCLAIMER: Typed it from the top of my head, so some minor things might be of with the code snippet.免责声明:从我的头顶输入它,所以一些小事情可能与代码片段有关。

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

相关问题 Java Executor-不并行运行线程 - Java Executor - not running threads in parallel Java Executor Service用于并行处理 - Java Executor Service for parallel processing Java中的任务执行程序,返回第一个结束任务 - Task executor in Java that returns first ending task Java Executor - 单线程多任务 - Java Executor - Single Thread Multiple Task ExecutorService.submit(Task)vs CompletableFuture.supplyAsync(Task,Executor) - ExecutorService.submit(Task) vs CompletableFuture.supplyAsync(Task, Executor) 在Java中捕获异常的并发排序任务执行器 - Concurrent sorted task executor that catches Exception in Java Grafana 仪表板将执行程序池的“boundedElastic”与“parallel”分开 - Grafana dashboard separating “boundedElastic” vs “parallel” for executor pool 执行程序内存与Java堆大小内存 - Executor Memory vs Java Heap Size memory 线程“ streaming-job-executor-0”中的异常java.lang.NoClassDefFoundError - Exception in thread “streaming-job-executor-0” java.lang.NoClassDefFoundError 具有后台和ui线程的线程池执行程序与android中的异步任务? - Thread pool executor with background and ui thread vs Async task in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM