0% found this document useful (0 votes)
21 views2 pages

Fork-Join Array Sum in Java

The document describes the implementation of an ArraySum class using the Fork-Join model for parallel computation. It details the SumTask class that recursively divides an array into smaller subtasks to calculate the sum, leveraging a ForkJoinPool for efficient task execution. Additionally, it explains the advantages of using a thread pool for task management over creating new threads for each task.

Uploaded by

hoangtu112201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Fork-Join Array Sum in Java

The document describes the implementation of an ArraySum class using the Fork-Join model for parallel computation. It details the SumTask class that recursively divides an array into smaller subtasks to calculate the sum, leveraging a ForkJoinPool for efficient task execution. Additionally, it explains the advantages of using a thread pool for task management over creating new threads for each task.

Uploaded by

hoangtu112201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# ArraySum.

java

Fork-Join Model: based on divide-and-conquer


fork: recursively break a task into smaller subtasks until they are simple enough
to run asynchronously.
join: solve each subtask and combine the results recursively

import [Link];
import [Link];

public class ArraySum {


private static class SumTask extends RecursiveTask<Long> {
private final int[] array;
private final int start, end;
private static final int THRESHOLD = 1000;

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


[Link] = array;
[Link] = start;
[Link] = end;
}

@Override
protected Long compute() {
int length = end - start;
if (length <= THRESHOLD) {
// Compute directly
long sum = 0;
for (int i = start; i < end; i++) {
sum += array[i];
}
return sum;
} else {
// Split task
int mid = start + length / 2;
SumTask leftTask = new SumTask(array, start, mid);
SumTask rightTask = new SumTask(array, mid, end);

// Fork subtasks
[Link]();
[Link]();
long rightResult = [Link]();
long leftResult = [Link]();

return leftResult + rightResult;


}
}
}

public static void main(String[] args) {


// Sample array
int[] array = new int[10_000];
for (int i = 0; i < [Link]; i++) {
array[i] = i + 1;
}

ForkJoinPool pool = new ForkJoinPool();


SumTask task = new SumTask(array, 0, [Link]);
long result = [Link](task);
[Link]("Sum of array elements: " + result);
}
}
- a special thread pool designed to work well with fork-join model.
- executes subtasks in parallel using a pool (fixed number) of worker threads.
- It uses a work-stealing algorithm, where idle threads "steal" tasks from busy
threads to improve efficiency.
- Often more efficient than manually creating threads for recursive tasks.
Thread Pool
- a pool of threads that can be reused to execute tasks, so that each thread may
execute more than one task.
- comparison: "creating a new thread per task" vs. "thread pool reusing existing
threads for task execution"
+ Creating a new thread comes with a performance overhead compared to reusing a
thread that is already created.
+ That is why reusing an existing thread to execute a task can result in a higher
total throughput than creating a new thread per task.
+ Thread pool makes it easy to control how many threads are active at a time.
+ Built-in thread pool: ExecutorService

You might also like