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?
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:
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
Then install the following JEdit Plugins:
Now, configure your Apache Ant:
SET JAVA_HOME=C:\Sanbox\jdk8
Time to configure JEdit Ant Plugin
Then create a new Java Project:
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:-)
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.
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.
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