ForkJoin is one of the neat and cool features introduced in Java 7. ForkJoin Algorithms are highly effective algorithms for resolving problems that follow "Recursive style ("Divide and Conquer Approach)" like FIbanocci, Merge Sort etc, where the tasks that are split to be processed are independent entities, and processed except for may be sharing only the output buffer with no other synchronization needed . Even though Merge sort task for eg, operate on a single buffer, they access different index's, for eg, when a task of 1000 entries, is divided in to 4 tasks, each task is processing different index ranges, and there is no need for synchronization.
Point to be noted is, the problem should be having specific characteristic to use the fork/join framework effectively.
1. The problem data(List/file/map...) to be processed and the computation on the data should be dividable to smaller subtasks.
2. Processing each of these sub tasks should be possible without requiring the result of other chunks. (except for waiting on Join for collating)
Fork Join algorithms are best suited for such class of application.
If you go through the documentation for ForkJoinTask, Restrictions are clearly documented. I have tried to list most of them here
1. ForkJoinTasks are intended to be used as "Computation tasks" for calculating pure functions (ie purely isolation objects.
2.Computations should avoid synchronized methods or blocks
3.Only allowed synchronizers are modern synchronizers such as Phasers that are advertised to cooperate with fork/join scheduling.
4. No other synchronization should be required except for synchronization by "Join".
5. Tasks should also not perform blocking IO, and should ideally access variables that are completely independent of those accessed by other running tasks.
Minor breaches of these restrictions may be tolerable (as all of these restriction cannot be clearly restricted by rules or thrown exceptions) in practice, but frequent misuse will result
in poor performance
ForkJoinTask - Where it cannot be used ?
Kavitha_Varadarajan 100000CEVG 1,368 Visits