ForkJoinPool ::FJP is a executor service for Fork-Join Taks or tasks which can computed using divide and conqor or we can say FJP is inbuild Map & Reduce framework in Java.

FJP implements work-steal so all threads try to find and execute task submitted by other tasks in FJP or external program. FJP try to maintain active threads as per number of processor available.

FJP provides below methods to submit task ::==>

Call Type External Task Clients i.e. main method Inner Task Divide and Conquer Calls Call from non-fork/join clients Call from within fork/join computations Arrange async execution execute(ForkJoinTask) ForkJoinTask.fork() Await and obtain result invoke(ForkJoinTask) ForkJoinTask.invoke() Arrange exec and obtain Future submit(ForkJoinTask) ForkJoinTask.fork() (ForkJoinTasks are Futures)









Pre-requiste :: FJP is in-builted in Java 7. You can download and install the same from Oracle.

You can either configure Java in your system path and also set JAVA_HOME to home directory of JDK installation.

Or you can use Eclipse and configure JRE in eclipse.

You can also use FJP with Java 6 but you need to do some configuration to make it work.

1st you need to download JSR166 from here.

Add JSR166 in your classpath in eclipse or using -cp when compling

And when running your program you need to pass VM Arguments as -Xbootclasspath/p:jsr166.jar in eclipse or using Java command line utility.

Getting Started ::

Classes which we use in our example

:: ForkJoinPool is used to create a pool of threads. You can pass number of processers as arguments. ForkJoinPool :: ForkJoinPool is used to create a pool of threads. You can pass number of processers as arguments.

:: RecursiveTask implements ForkJoinTask and its compute method computes the result and return a result of type V. RecursiveTask<V> :: RecursiveTask implements ForkJoinTask and its compute method computes the result and return a result of type V.

:: Similar to RecursiveTask but its compute method doesn't return anything means void. RecursiveAction :: Similar to RecursiveTask but its compute method doesn't return anything means void.

:: Superclass of RecursiveAction and RecursiveTaks. Provides methods like fork, join, invokeAll to create subtaks from current task and submit to Pool. ForkJoinTask :: Superclass of RecursiveAction and RecursiveTaks. Provides methods like fork, join, invokeAll to create subtaks from current task and submit to Pool.

Creating ForkJoinPool

You can create ForkJoinPool as

ForkJoinPool fjp = new ForkJoinPool(); // Use all available processors

or

ForkJoinPool fjp = new ForkJoinPool(numperOfProcessors); // Use number of processors passed

Creating RecursiveTask<V>

In RecursiveTask compute method is similar to run method of Thread/Runnable and call method of Callable interface.

So we will not call compute method ourselves. But all logic to divide big task into smaller task will be in compute method.

You need to create a subclass of RecursiveTask<V> to start get going. Also we need to pass data to newly created class.

So best way is to create parameterized constuctor. And Store the data at instance level. So compute method can access the data and work on it.

Code With Comments