Using the ForkJoin Framework
First and foremost, read Dan's intro
to the framework; reading and following its directions can save a fair
amount of anguish later on. The steps listed in this html page are in
part a summary of Dan's text, though some is added and some is left out.
There are alternatives to the steps below, but it'll save you a headache
to follow through these the first time, even if they differ from the
way you normally do things in eclipse.
- Download the jar here
- In Eclipse, create a new project, and call it whatever you like
- Outside of Eclipse, copy the file jsr166.jar into your project directory
- Now for some settings: go to Project->Properties and under "Java
Compiler" check "Enable project specific settings", and make sure
everything is set to Java 1.6 (if you don't have 1.6 installed, you'll
need to do so before proceeding)
- Still in Project->Properties, go to "Java Build Path", then
Libraries, and 'Add External JARs'. Select the jsr166.jar you placed inside the project directory
- In your new project, create a new 'Source Folder', if one doesn't show up already
- Create/add your class to the source folder; make sure it has a main method:
- Your class should extend RecursiveTask or RecursiveAction
- Once in your program, and only once, before you start
parallelizing, you need to startup the forkjoin pool. It can be done in
main, or you can just have it as a public static variable: static final
ForkJoinPool fjPool=new ForkJoinPool();
- In main(), you start up the entire process via fjPool.invoke(new ForkJoinTemplate(a,0,num)), or whatever its arguments are
- You need a particular run configuration for this whole thing to
work: Create a new run configuration and under arguments, in VM
Arguments, put the following: -Xbootclasspath/p:jsr166.jar
Troubleshooting
I ran into a number of possibilities setting this set up the first time,
so allocate a bit of extra time to get it up and running. If you run
into problems:
- NoClassDefFoundError or java.lang.SecurityException when you try to run may
mean that the 'VM Argument' isn't set correctly; it must be exactly
'-Xbootclasspath/p:jsr166.jar'
- Out of memory, stack overflow, etc.: It's running, but runs out of
memory (as you may have divined from the error message); up the cutoff,
cut down on the size of the input, etc. Another possibility is that
it's not switching to your sequential case properly; try checking that
code
- If your jar file is not in your project dir, or not listed as a library, it most likely won't work