Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel Extensions Equivalent in Java

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?

like image 573
pnewhook Avatar asked Nov 17 '25 00:11

pnewhook


2 Answers

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;
like image 176
Apocalisp Avatar answered Nov 19 '25 14:11

Apocalisp


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

like image 25
J-16 SDiZ Avatar answered Nov 19 '25 13:11

J-16 SDiZ