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?
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;
There are some limited support in java.util.concurrent package since java 5, but full supports are java 7 feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With