Java中与Scala中的并行集合接近吗? [英] Is there anything in Java close to the parallel collections in Scala?
问题描述
使用Java实现并行计算(例如在多核处理器上)的最简单方法是什么. IE.与此Scala代码等效的Java
What is the simplest way to implement a parallel computation (e.g. on a multiple core processor) using Java. I.E. the java equivalent to this Scala code
val list = aLargeList
list.par.map(_*2)
有此库,但似乎不胜枚举.
推荐答案
http://gee. cs.oswego.edu/dl/jsr166/dist/extra166ydocs/
别这么快就放弃 ,快点! ))
Don't give up so fast, snappy! ))
来自Javadocs (更改后将映射到您的f )本质上就是这样:
From the javadocs (with changes to map to your f) the essential matter is really just this:
ParallelLongArray a = ... // you provide
a.replaceWithMapping (new LongOp() { public long op(long a){return a*2L;}};);
这差不多吧?
val list = aLargeList
list.par.map(_*2)
&如果您愿意以简洁的方式生活,那么上面的内容可能是一个相当干净清晰的3衬里(当然,如果您重复使用函数,那么它与Scala完全相同-内联函数.):
& If you are willing to live with a bit less terseness, the above can be a reasonably clean and clear 3 liner (and of course, if you reuse functions, then its the same exact thing as Scala - inline functions.):
ParallelLongArray a = ... // you provide
LongOp f = new LongOp() { public long op(long a){return a*2L;}};
a.replaceWithMapping (f);
[上面编辑以显示简洁完整的ala OP Scala变体形式]
[edited above to show concise complete form ala OP's Scala variant]
这里是最大冗长的形式,我们从头开始进行演示:
and here it is in maximal verbose form where we start from scratch for demo:
import java.util.Random;
import jsr166y.ForkJoinPool;
import extra166y.Ops.LongGenerator;
import extra166y.Ops.LongOp;
import extra166y.ParallelLongArray;
public class ListParUnaryFunc {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
// create a parallel long array
// with random long values
ParallelLongArray a = ParallelLongArray.create(n-1, new ForkJoinPool());
a.replaceWithGeneratedValue(generator);
// use it: apply unaryLongFuncOp in parallel
// to all values in array
a.replaceWithMapping(unaryLongFuncOp);
// examine it
for(Long v : a.asList()){
System.out.format("%d\n", v);
}
}
static final Random rand = new Random(System.nanoTime());
static LongGenerator generator = new LongGenerator() {
@Override final
public long op() { return rand.nextLong(); }
};
static LongOp unaryLongFuncOp = new LongOp() {
@Override final public long op(long a) { return a * 2L; }
};
}
最终编辑和注释:
还请注意一个简单的类,如下所示(您可以在项目中重复使用):
Also note that a simple class such as the following (which you can reuse across your projects):
/**
* The very basic form w/ TODOs on checks, concurrency issues, init, etc.
*/
final public static class ParArray {
private ParallelLongArray parr;
private final long[] arr;
public ParArray (long[] arr){
this.arr = arr;
}
public final ParArray par() {
if(parr == null)
parr = ParallelLongArray.createFromCopy(arr, new ForkJoinPool()) ;
return this;
}
public final ParallelLongArray map(LongOp op) {
return parr.replaceWithMapping(op);
}
public final long[] values() { return parr.getArray(); }
}
类似的东西将使您可以编写更流畅的Java代码(如果简洁对您而言很重要):
and something like that will allow you to write more fluid Java code (if terseness matters to you):
long[] arr = ... // you provide
LongOp f = ... // you provide
ParArray list = new ParArray(arr);
list.par().map(f);
当然可以推动上述方法以使其更加整洁.
And the above approach can certainly be pushed to make it even cleaner.
这篇关于Java中与Scala中的并行集合接近吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!