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() ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.