Reputation: 151
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
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
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
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:
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.
Upvotes: 8
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
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