Skip to content

Latest commit

 

History

History
85 lines (71 loc) · 3.66 KB

04-fork-join.asc

File metadata and controls

85 lines (71 loc) · 3.66 KB

Fork/Join Framework

Objective
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.

  1. You can implement a RecursiveAction that divides a large task into smaller parts.

    src/org/j6toj8/concurrency/forkjoin/ForkJoin_RecursiveAction.java
    link:../../../src/org/j6toj8/concurrency/forkjoin/ForkJoin_RecursiveAction.java[role=include]
    console output
    ForkJoinPool-1-worker-1 - ABCDEF
    ForkJoinPool-1-worker-4 - TUVWXYZ
    ForkJoinPool-1-worker-2 - NOPQRS
    ForkJoinPool-1-worker-3 - GHIJKLM
    1. This example creates a task of type RecursiveAction called StringPrinting.

    2. This RecursiveAction gets a String to print to the Console.

    3. In the compute method, the job decides whether to print directly to the console, or to split this job into two parts: if the String 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.

  2. 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.java
    link:../../../src/org/j6toj8/concurrency/forkjoin/ForkJoin_RecursiveTask.java[role=include]
    console output
    ForkJoinPool-1-worker-3 - NOPQRS
    ForkJoinPool-1-worker-2 - GHIJKLM
    ForkJoinPool-1-worker-4 - ABCDEF
    ForkJoinPool-1-worker-1 - TUVWXYZ
    Resultado da execução: 26
    1. This example creates a task of type RecursiveTask called StringPrinting.

    2. This RecursiveTask is given a String to print to the Console.

    3. In the compute method, the job decides whether to print directly to the console, or to split this job into two parts: if the String 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.

References