問題描述
我想知道是否有人可以指出我在 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 等效項的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!