Use the parallel Fork/Join Framework
With the Fork/Join framework you can split a large task into smaller pieces and execute them in parallel. The use of the framework is simple. Creating broken tasks into several parts can be a bit more complex.
A task usually follows a default behavior:
-
Receives a value;
-
Evaluates if the value is small enough to perform the task with it;
-
If so, execute the task with that value;
-
If not, it breaks into one or more parts and creates new smaller tasks;
-
-
Continues running recursively.
Examples will be presented using the RecursiveAction
class, which returns no values, so its implementation is simpler. And using the RecursiveTask
class, which returns values and so its implementation is a bit more complex.
The examples are large because you need to implement the entire RecursiveAction
or RecursiveTask
class for the example to work. Therefore, the steps will be described with comments in the code, explaining the operation of each call made to the framework.
-
You can implement a
RecursiveAction
that divides a large task into smaller parts.src/org/j6toj8/concurrency/forkjoin/ForkJoin_RecursiveAction.javalink:../../../src/org/j6toj8/concurrency/forkjoin/ForkJoin_RecursiveAction.java[role=include]
console outputForkJoinPool-1-worker-1 - ABCDEF ForkJoinPool-1-worker-4 - TUVWXYZ ForkJoinPool-1-worker-2 - NOPQRS ForkJoinPool-1-worker-3 - GHIJKLM
-
This example creates a task of type
RecursiveAction
calledStringPrinting
. -
This
RecursiveAction
gets aString
to print to the Console. -
In the
compute
method, the job decides whether to print directly to the console, or to split this job into two parts: if theString
is less than 10 characters long, it prints directly to the console; otherwise it splits the work into two new tasks.Note that each print on the console is performed on a different thread, making it clear that the job is being shared by multiple threads.
Also note that String processing returns no value, so the
RecursiveAction
class was used.
-
-
If you need to return a value, you can implement a
RecursiveTask
that divides a large task into smaller parts.src/org/j6toj8/concurrency/forkjoin/ForkJoin_RecursiveTask.javalink:../../../src/org/j6toj8/concurrency/forkjoin/ForkJoin_RecursiveTask.java[role=include]
console outputForkJoinPool-1-worker-3 - NOPQRS ForkJoinPool-1-worker-2 - GHIJKLM ForkJoinPool-1-worker-4 - ABCDEF ForkJoinPool-1-worker-1 - TUVWXYZ Resultado da execução: 26
-
This example creates a task of type
RecursiveTask
calledStringPrinting
. -
This
RecursiveTask
is given aString
to print to the Console. -
In the
compute
method, the job decides whether to print directly to the console, or to split this job into two parts: if theString
is less than 10 characters long, it prints directly to the console; otherwise it splits the work into two new tasks.Also note that String processing returns how many characters were printed, so the
RecursiveTask
class was used.
-
-
Managing Concurrent Processes
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 377). Wiley. Kindle Edition.
-
Fork/Join. The Java™ Tutorials.