且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java pool.map/ Multiprocessing的Java等价物

更新时间:2022-06-19 18:14:13

没有完全兼容的类,但 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.)