ForkJoinPool

ForkJoinPool is the core of the concurrency frameworks which follows divide and conquer approach in completing the tasks.

Fork – Recursively break the single task into multiple smaller tasks till the threshold.
Join – Join the multiple smaller tasks results and produce the output recursively.

Initialise the maximum number of worker threads.

RecursiveAction class is extended to recursively split the inputs and put in the ForkJoinPool. compute() method will be called, when the recursive action class is invoked by ForkJoinTask.
Below statement will call the compute method of recursive objects again to split further recursively. This will split the content into different sub content, and after execution of worker threads, the task result will be joined.

invoke() method forks the task and waits for the result, and it doesn’t require any manual join, and will return the task result.

Code:

Result:

 

Leave a Reply

Your email address will not be published. Required fields are marked *