How it works...

In this recipe, you implemented the MyWorkerTask class that extends the ForkJoinTask class. It's your own base class to implement tasks that can be executed in a ForkJoinPool executor and that can take advantage of all the benefits of the executor, as it's a work-stealing algorithm. This class is equivalent to the RecursiveAction and RecursiveTask classes.

When you extend the ForkJoinTask class, you have to implement the following three methods:

  • setRawResult(): This method is used to establish the result of the task. As your tasks don't return any results, leave this method empty.
  • getRawResult(): This method is used to return the result of the task. As your tasks don't return any results, this method returns null.
  • exec(): This method implements the logic of the task. In this case, you delegated the logic to the abstract compute() method (as the RecursiveAction and RecursiveTask classes). However, in the exec() method, you measure the execution time of the method, writing it in the console.

Finally, in the main class of the example, you created an array of 10,000 elements, a ForkJoinPool executor, and a Task object to process the whole array. Execute the program and you'll see how the different tasks that are executed write their execution time in the console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.100.219