Fork-Join — Explained with Examples
Fork-join is a parallel execution model where tasks split (fork) into subtasks and later merge (join) results after completion.
Fork-join divides a larger task into smaller subtasks (fork), executes them in parallel when possible, and then combines their results (join). This maps naturally to divide-and-conquer algorithms. Java’s ForkJoinPool, Python’s concurrent.futures, and OpenMP implement this model. The fork step creates new work items; the join step synchronizes and aggregates.
Think of fork-join like organizing a group research project. The leader forks the work: one person researches history, another researches technology, a third researches economics. Each works independently (parallel). When all finish, they join to compile the final report (combine results). The total time is the longest individual task, not the sum of all tasks.
Fork-join is particularly effective for recursive algorithms where subproblems are independent. Work-stealing schedulers dynamically balance load across worker threads.
from concurrent.futures import ProcessPoolExecutor, as_completed
def merge_sort_parallel(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
with ProcessPoolExecutor(max_workers=2) as executor:
# Fork
left_future = executor.submit(merge_sort_parallel, arr[:mid])
right_future = executor.submit(merge_sort_parallel, arr[mid:])
# Join
left = left_future.result()
right = right_future.result()
# Combine
return merge(left, right)
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
import random
data = [random.randint(0, 1000) for _ in range(100)]
sorted_data = merge_sort_parallel(data)
print(sorted_data[:10]) # First 10 sorted elementsFork-join works best when subtasks are CPU-bound and independent. Overhead from task creation and synchronization can outweigh benefits for small tasks.
Thread, Concurrency vs Parallelism, Divide and Conquer, Process
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro