0% found this document useful (0 votes)
0 views

Fork in Java

Notes about fork concept of java

Uploaded by

Deepa Deepa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Fork in Java

Notes about fork concept of java

Uploaded by

Deepa Deepa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

In Java, fork typically refers to the use of the Fork/Join Framework, introduced in Java 7,

which is a part of the java.util.concurrent package. It is used for parallel processing,


allowing tasks to be broken into smaller subtasks, executed concurrently, and then combined to
produce the result.

Here’s a quick overview of Fork/Join:

Key Concepts

1. Fork: Splits a task into smaller subtasks that can run concurrently.
2. Join: Combines the results of the subtasks once they’re complete.

Components

1. ForkJoinPool: A specialized thread pool that manages and executes Fork/Join tasks. It
uses a work-stealing algorithm where idle threads can "steal" tasks from others to
optimize CPU usage.
2. ForkJoinTask: The base class for tasks that can be executed in the Fork/Join framework.
There are two primary subclasses:
o RecursiveTask: Used for tasks that return a result.
o RecursiveAction: Used for tasks that do not return a result.

import java.util.concurrent.RecursiveTask;
import java.util.concurrent.ForkJoinPool;

class SumTask extends RecursiveTask<Long> {


private static final int THRESHOLD = 10; // Smallest unit of work
private int[] numbers;
private int start;
private int end;

public SumTask(int[] numbers, int start, int end) {


this.numbers = numbers;
this.start = start;
this.end = end;
}

@Override
protected Long compute() {
if (end - start <= THRESHOLD) {
// Perform computation directly
long sum = 0;
for (int i = start; i < end; i++) {
sum += numbers[i];
}
return sum;
} else {
// Split task into smaller subtasks
int middle = (start + end) / 2;

SumTask leftTask = new SumTask(numbers, start, middle);


SumTask rightTask = new SumTask(numbers, middle, end);

leftTask.fork(); // Fork the left subtask


long rightResult = rightTask.compute(); // Compute the right subtask directly
long leftResult = leftTask.join(); // Wait for the left subtask to finish

return leftResult + rightResult; // Combine the results


}
}
}

public class ForkJoinExample {


public static void main(String[] args) {
int[] numbers = new int[100];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1; // Fill array with numbers 1 to 100
}

ForkJoinPool pool = new ForkJoinPool();


SumTask task = new SumTask(numbers, 0, numbers.length);

long result = pool.invoke(task); // Start the computation


System.out.println("Sum: " + result); // Output: 5050
}
}

How It Works

1. Thresholding: If the task size is below the threshold (e.g., 10 numbers), it computes
directly.
2. Splitting: Otherwise, it splits the task into smaller subtasks (left and right halves).
3. Parallel Execution: One subtask is executed in parallel (forked), while the other is
computed in the current thread.
4. Result Aggregation: The results of the subtasks are joined to form the final result.

Benefits

 Efficiently uses available CPU cores.


 Simplifies parallel processing for divide-and-conquer problems.
Use Cases

 Processing large data sets (e.g., sorting, searching).


 Recursive algorithms (e.g., Fibonacci, matrix multiplication).
 Tasks that can benefit from parallelism.

Let me know if you'd like to explore this further!

n Java, the fork/join framework is a way to achieve parallelism by splitting a


large task into smaller subtasks that can be executed concurrently. This
framework is particularly useful for tasks that can be divided into smaller,
independent subtasks.

Here's how it works:


 Fork:
A task can split itself into smaller subtasks using the fork() method. These subtasks
are then executed concurrently on different threads, allowing the program to take
advantage of multiple CPU cores.
 Join:
Once all subtasks are completed, the results are combined using the join() method.
Key classes and interfaces:
 ForkJoinPool: The core of the framework, a thread pool designed for handling fork/join
tasks.
 ForkJoinTask: An abstract class that represents a task that can be forked.
 RecursiveAction: A subclass of ForkJoinTask that does not return a result.
 RecursiveTask: A subclass of ForkJoinTask that returns a result.

You might also like