Java pool.map/ Multiprocessing的Java等价物 [英] Java equivalent for Python pool.map/ Multiprocessing

查看:174
本文介绍了Java 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.

更新:

这似乎表明线程是要走的路。我希望我别无选择,只能涉足一堆锁(意外的双关语)并等待我的船开航。尽管如此,欢迎使用简单的例子。

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 >集合并等待结果,但您可以轻松地从集合< Callable< T>> > Callable< T> 和 Collection< T> ,然后只需调用 invokeAll ,其中返回列表< Future< 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 的函数,你需要循环提交,然后构建你自己的东西集合等待但是 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.)

这篇关于Java pool.map/ Multiprocessing的Java等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆