-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForkJoinExample.java
More file actions
107 lines (92 loc) · 2.74 KB
/
Copy pathForkJoinExample.java
File metadata and controls
107 lines (92 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package java7.forkjoin;
import java.util.concurrent.*;
/**
* Java 7 Fork/Join Framework Example Demonstrates parallel processing
*/
public class ForkJoinExample
{
public static void main(String[] args)
{
System.out.println("=== Java 7 Fork/Join Framework ===\n");
// Create array for summing
int[] array = new int[10000];
for (int i = 0; i < array.length; i++)
{
array[i] = i + 1;
}
System.out.println("1. Fork/Join Sum Calculation");
System.out.println("---------------------------");
ForkJoinPool pool = new ForkJoinPool();
SumTask task = new SumTask(array, 0, array.length);
long result = pool.invoke(task);
System.out.println("Sum of 1 to 10000: " + result);
System.out.println("Expected: " + (10000L * 10001L / 2L));
System.out.println("\n2. Fork/Join Concept");
System.out.println("------------------");
System.out.println("""
Fork/Join Framework:
- Divide task into smaller subtasks
- Fork: Split task and execute in parallel
- Join: Combine results from subtasks
- Work-stealing algorithm for load balancing
""");
System.out.println("\nKey Features:");
System.out.println("- Divide-and-conquer parallelism");
System.out.println("- Work-stealing algorithm");
System.out.println("- Efficient for recursive tasks");
System.out.println("- Automatic load balancing");
System.out.println("\nUse Cases:");
System.out.println("- Parallel sorting");
System.out.println("- Parallel searching");
System.out.println("- Parallel processing");
System.out.println("- Recursive algorithms");
System.out.println("\nBenefits:");
System.out.println("- Better CPU utilization");
System.out.println("- Automatic load balancing");
System.out.println("- Efficient for recursive tasks");
System.out.println("- Easy to use");
}
}
// RecursiveTask for computing sum
class SumTask extends RecursiveTask<Long>
{
private static final int THRESHOLD = 1000;
private int[] array;
private int start;
private int end;
public SumTask(int[] array, int start, int end)
{
this.array = array;
this.start = start;
this.end = end;
}
@Override
protected Long compute()
{
int length = end - start;
if (length < THRESHOLD)
{
// Direct computation for small arrays
long sum = 0;
for (int i = start; i < end; i++)
{
sum += array[i];
}
return sum;
}
else
{
// Fork: Split into subtasks
int mid = start + length / 2;
SumTask leftTask = new SumTask(array, start, mid);
SumTask rightTask = new SumTask(array, mid, end);
// Fork left task (execute in parallel)
leftTask.fork();
// Compute right task and join left task
long rightResult = rightTask.compute();
long leftResult = leftTask.join();
// Combine results
return leftResult + rightResult;
}
}
}