Java equivalent for Python pool.map/ Multiprocessing(Python pool.map/Multiprocessing 的 Java 等效项)
问题描述
我想知道是否有人可以指出我在 java 中的一个简单等效的 python 多处理模块.
I was wondering if somebody could point me to a simple equivalent of python's multiprocessing module in java.
我有一个简单的并行处理场景(其中没有 2 个进程交互):获取一个数据集并将其拆分为 12 个并将 java 方法应用于 12 个数据集,收集结果并将它们加入某种列表中相同的顺序.
I have a simple parallel processing scenario (where no 2 processes interact): Take a data set and split it into 12 and apply a java method to the 12 datasets, collect results and join them in a list of some sort with the same ordering.
作为专业"语言的 Java 似乎有多个库和方法 - 谁能帮助这个 Java 新手入门?
Java being a "pro" language appears to have multiple libraries and methods - anyone who can help this java newbie get started?
我想用最少的代码来做这件事 - 正如我所说的,我的要求非常简单.
I would like to do this with minimal of coding - as i said my requirement is pretty straightforward.
更新:如何在 java 中进行多处理,以及预计速度会提高多少?
这似乎表明线程是要走的路.我希望我别无选择,只能涉入一堆船闸(双关语无意)并等待我的船航行.尽管如此,还是欢迎简单的例子.
This seems to indicate threads is the way to go. I expect I have no choice but wade into a bunch of locks (pun unintended) and wait for my ship to sail. Simple examples are welcome nevertheless.
推荐答案
没有完全兼容的类,但是 ExecutorService
为您提供实现它所需的一切.
There's no exactly-compatible class, but ExecutorService
gives you everything you need to implement it.
特别是,没有将 Callable
映射到 Collection
并等待结果的函数,但您可以轻松构建 Collection<Callable<T>>
出 Callable<T>
和 Collection
invokeAll
,它会返回一个列表<未来<T>>
.
In particular, there's no function to map a Callable
over a Collection
and wait on the results, but you can easily build a Collection<Callable<T>>
out of a Callable<T>
and Collection<T>
, then just call invokeAll
, which returns you a List<Future<T>>
.
(如果您想模拟 multiprocessing.Pool
中的一些其他函数,则需要循环 submit
并构建自己的等待集合开.但是map
很简单.)
(If you want to emulate some of the other functions from multiprocessing.Pool
, you will need to loop around submit
instead and build your own collection of things to wait on. But map
is simple.)
这篇关于Python pool.map/Multiprocessing 的 Java 等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python pool.map/Multiprocessing 的 Java 等效项
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01