Zixelll
Zixelll

Reputation: 151

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?

Upvotes: 15

Views: 2404

Answers (5)

rai.skumar
rai.skumar

Reputation: 10667

Below example finds all directories in my documents directory:

List<File> directories = Arrays.asList(new File("/Users/sid/Documents")
                               .listFiles())
                               .parallelStream()
                               .filter(t -> t.isDirectory() == true)
                               .collect(Collectors.toList());

Java 8 provides stream support where a collection is transformed into a continuous stream of objects. If the size of the collection is small .stream() is alright. But if you have a big collections and want to exploit the parallelism feature then you can use .parallelStream() method.

Upvotes: 0

Michael Berry
Michael Berry

Reputation: 72254

You can use the nightly version of Netbeans which now has some experimental support for JDK8 features - I've given this a try and it seems to work well with lambdas (at least you don't get the red squiggles under them, auto-formatting and suggested corrections don't seem to work properly yet, but they're more in the way of minor niggles.) You'll need to make sure you add the Lambda enabled JDK8 as a Java Platform, and then set the source level to Java 8 for the project you want to experiment with.

You can grab the latest Lambda enabled build of the JDK here.

At the time of writing, there are 3 types of parallel methods on the static Arrays class which can be experimented with - parallelStream(), parallelPrefix(), and parallelSort(). Note however that this will likely change before the final release, the API at present is very much in flux.

Upvotes: 2

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78579

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:

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.

Upvotes: 8

Sri Harsha
Sri Harsha

Reputation: 319

Check if your netbeans is using jdk8 (i doubt it) . If it does not then make it point to your local copy of jdk8 instead of the inbuilt jdk. Hope this helps.

Upvotes: 2

Stephen C
Stephen C

Reputation: 718708

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.

Upvotes: 2

Related Questions