Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallelism in Java 8

I tried to use new parallel feature JDK8, but unfortunately, I couldn't get it to work. NetBeans 7.1 says that method "parallel" does not exist.

Does this method require special import? Does anyone have sample code demonstrating Java 8 parallelism?

like image 407
Zixelll Avatar asked Sep 02 '12 07:09

Zixelll


2 Answers

I have been playing with JDK8 Lambda Developer Preview for a few weeks now. The following is what I do to simplify compilation and testing of my code:

Configure JEdit to Compile JDK 8 Code

The following guide describes how to configure Apache Ant and JEdit to easily compile source code with JDK 8 Lambda Expressions and the new API features in the JDK 8 Lambda Developer Preview.

This is what I do, as of today, basically because no IDE supports these JDK 8 features yet.

Download the following:

  • JDK 8
  • JEdit
  • Apache Ant

Then create the following directory structure:

Sanbox
|-----jdk8
|-----ant
|-----projects
  • Place the uncompressed JDK build in the jdk8 directory.
  • Place the uncompressed Apache Ant in the ant directory.
  • The projects directory will be for JEdit projects.

Then install the following JEdit Plugins:

  • Ant Farm
  • Java Fold
  • Project Builder
  • Project Viewer
  • Project Wizard
  • SVN Plugin (I use this to synchronize my projects with my repo, you may not need it, though)

Now, configure your Apache Ant:

  • Create a file in your home folder named antrc_pre.bat (i.e. %USERPROFILE%\antrc_pre.bat).
  • (Note: if you are using Linux you can configure this in ~/.ant/ant.conf).
  • This file will be run by Apache Ant before running any tasks, therefore, this is a place to configure or override which JDK you want to use by defining the JAVA_HOME variable.
  • At the top of this file define the JAVA_HOME variable and make it point to the directory where the JDK8 is installed. Somewhat like this: SET JAVA_HOME=C:\Sanbox\jdk8
  • Make sure to comment it out once you're done with your JDK 8 session so that Ant continues to use the default configuration.

Time to configure JEdit Ant Plugin

  • In JEdit go to Plugins -> Plugin Options -> Ant Farm -> Build Options
  • In the dialog select the option: "Run Ant targets using an external script/build file"
  • Choose the ant.bat script (i.e. C:\Sandbox\ant\bin\ant.bat).
  • Note: If you are using Ant 1.8.x it is probable that you'll need to add a property in the properties section of the plugin: build.compiler=javac1.7, otherwise you would get an error at compiling with JDK 8. I did not have this problem with Ant 1.7, though.

Then create a new Java Project:

  • In JEdit go to Plugins -> Project Builder -> Create New Project
  • Choose Java Application and click Next
  • Choose your projects directory as the place to locate files (i.e. C:\Sanbox\projects).

Voila! At this point, JEdit will present four buttons in the tool bar: Build Application, Compile, Clean and Run Application. These are based on the build.xml file and are executed according to the corresponding Ant tasks. You're good to go, you may start writing lambda expressions and use the new APIs:-)

Parallelism Example

In the last developer preview (b50), there is little paralleism implemented yet. I can see they are doing more work in a seperate branch (if you want to download and build the OpenJDK8 source, though).

You can, however, use a method Arrays.parallell which creates a ParallelIterable wrapper over an array. This you can use to test some of the parallelism features.

I did an example to find primes in a large array. I could verify that all my four cores are used when I run this in parallel.

Integer[] source = new Integer[30000000];
for(int i=0; i<source.length; i++)
    source[i] = i;

ParallelIterable<Integer> allIntegers = Arrays.parallel(source).filter(isPrime);
Iterable<Integer> primes = allIntegers.into(new LinkedList<Integer>());

This compiles and runs fine in my JEdit Project with Apache Ant 8.4.x and JDk8-b50.

I hope this helps.

PD:

I did not define the predicate isPrime in the code above in order not to obscure the simplicity of the example. I am pretty sure eveyone can easily define a primality predicate to try this code.

like image 159
Edwin Dalorzo Avatar answered Oct 05 '22 23:10

Edwin Dalorzo


My suggestion would be to put Netbeans to one side, use a plain text editor to edit your Java code, and compile and run it from the command prompt, using the Java 8 toolchain. That way you can be sure that your problems are not due to a Netbeans issue.

like image 30
Stephen C Avatar answered Oct 06 '22 01:10

Stephen C