views:

288

answers:

4

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?

+2  A: 

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

J-16 SDiZ
+2  A: 

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;
Apocalisp
+1  A: 

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.

Chris B
+1  A: 

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

Max