ForkJoin Idea
ForkJoinPool divides tasks recursively (fork) and combines results (join). It is well-suited for divide-and-conquer scenarios such as parallel sorting.
Know the patterns, not every API detail.
Work-stealing pool: ForkJoinPool [1].
Where used
Parallel Streams, recursive parallelization of divide-and-conquer, work stealing in the JVM. It is suitable for merge-sort-like tasks on multi-core machines.
Depth
The ForkJoin framework breaks a large computation into smaller tasks. Fork makes a subtask available for potential parallel processing, and join waits for its result. Small tasks are calculated directly to limit overhead and excessive subdivision.
Workers employ work stealing: An idle worker takes tasks from the queue of a busy worker. This dynamically balances uneven subtrees. It is well-suited for largely independent, CPU-intensive computations with manageable result combinations.
Difficulty levels
- Determine the base case and decomposition threshold of a task.
- Compute one part directly and execute another in parallel.
- Adjust granularity and load distribution for actual costs.
Pitfalls
Blocking I/O within many tasks can slow down the worker pool. A threshold that is too small incurs more scheduling costs than computational gains, while a threshold that is too large leaves parallelism unused.
Sources
Tasks
Card Info
- Topic: Algorithms and Data Structures
- Difficulty: Beginner
- Completed: 0 users