Parallel Extensions Equivalent in Java

若如初见. 提交于 2019-12-07 14:11:50

问题


I have some experience using parallel extensions in .Net development, but I was looking at doing some work in Java that would benefit from an easy to use parallelism library. Does the JVM offer any comparable tools to parallel-extensions?


回答1:


There are some limited support in java.util.concurrent package since java 5, but full supports are java 7 feature.




回答2:


You should become familiar with the java.util.concurrent package. It has simple and robust primitives for parallel programming upon which to base higher-level libraries. One example of such a library is Functional Java, which has an easy-to-use module for parallel programming.

For example, here is the canonical MapReduce example written using Functional Java. It counts the number of words in a set of documents, assuming documents are Streams of characters:

public static long countWords(final List<Stream<Character>> documents,
                              final ParModule m)
{ return m.parFoldMap(documents,
                      new F<Stream<Character>, Long>()
                      { public Long f(final Stream<Character> document)
                        { return (long)fromStream(document).words().length(); }},
                      longAdditionMonoid)
          .claim(); }

To instantiate ParModule, you give it a parallel Strategy. You can implement your own Strategies, or use one that's supplied. Here's one that uses a fixed pool of 16 threads:

ExecutorService pool = newFixedThreadPool(16);
ParModule m = parModule(executorStrategy(pool));

You need the following imports for the example above:

import fj.F;
import fj.data.Stream;
import fj.control.parallel.ParModule;
import static fj.control.parallel.ParModule.parModule;
import static fj.pre.Monoid.longAdditionMonoid;
import static fj.data.LazyString.fromStream;     
import static fj.control.parallel.Strategy.executorStrategy;
import static java.util.concurrent.Executors.newFixedThreadPool;
import java.util.concurrent.ExecutorService;



回答3:


you should definetely try Ateji Parralel extensions. http://www.ateji.com/multicore/




回答4:


Here is the site for the JSR166 working group, who wrote the new concurrency classes for Java 7.

http://g.oswego.edu/dl/concurrency-interest/

There are 2 packages listed: jsr166y (the new concurrency classes for Java 7) and extra166y (various classes that for one reason or another will not be included).

One of the classes in extra166y, ParallelArray is very useful. It automatically parallelizes binary searches, merge-sorts, etc.



来源:https://stackoverflow.com/questions/1077584/parallel-extensions-equivalent-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!