Python pool.map/Multiprocessing 的 Java 等效项

Java equivalent for Python pool.map/ Multiprocessing(Python pool.map/Multiprocessing 的 Java 等效项)

本文介绍了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 等效项

基础教程推荐