-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum max .java
More file actions
174 lines (121 loc) · 3.69 KB
/
Copy pathSum max .java
File metadata and controls
174 lines (121 loc) · 3.69 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.util.*;
import java.util.concurrent.*;
public class ParallelReduction {
// Parallel Sum
static class SumTask implements Callable<Integer> {
int[] arr;
int start, end;
SumTask(int[] arr, int start, int end) {
this.arr = arr;
this.start = start;
this.end = end;
}
public Integer call() {
int sum = 0;
for (int i = start; i < end; i++) {
sum += arr[i];
}
return sum;
}
}
// Parallel Min
static class MinTask implements Callable<Integer> {
int[] arr;
int start, end;
MinTask(int[] arr, int start, int end) {
this.arr = arr;
this.start = start;
this.end = end;
}
public Integer call() {
int min = arr[start];
for (int i = start; i < end; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
}
// Parallel Max
static class MaxTask implements Callable<Integer> {
int[] arr;
int start, end;
MaxTask(int[] arr, int start, int end) {
this.arr = arr;
this.start = start;
this.end = end;
}
public Integer call() {
int max = arr[start];
for (int i = start; i < end; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
}
public static void main(String[] args) throws Exception {
int[] arr = {
10, 20, 30, 40,
50, 60, 70, 80
};
int threads = 4;
ExecutorService executor =
Executors.newFixedThreadPool(threads);
List<Future<Integer>> sumResults =
new ArrayList<>();
List<Future<Integer>> minResults =
new ArrayList<>();
List<Future<Integer>> maxResults =
new ArrayList<>();
int chunk = arr.length / threads;
// Create Tasks
for (int i = 0; i < threads; i++) {
int start = i * chunk;
int end = (i == threads - 1)
? arr.length
: start + chunk;
sumResults.add(
executor.submit(
new SumTask(arr, start, end)));
minResults.add(
executor.submit(
new MinTask(arr, start, end)));
maxResults.add(
executor.submit(
new MaxTask(arr, start, end)));
}
// Reduction for Sum
int totalSum = 0;
for (Future<Integer> f : sumResults) {
totalSum += f.get();
}
// Reduction for Min
int minimum = Integer.MAX_VALUE;
for (Future<Integer> f : minResults) {
minimum = Math.min(minimum, f.get());
}
// Reduction for Max
int maximum = Integer.MIN_VALUE;
for (Future<Integer> f : maxResults) {
maximum = Math.max(maximum, f.get());
}
// Average
double average =
(double) totalSum / arr.length;
executor.shutdown();
// Output
System.out.println("Array: "
+ Arrays.toString(arr));
System.out.println("Sum = "
+ totalSum);
System.out.println("Minimum = "
+ minimum);
System.out.println("Maximum = "
+ maximum);
System.out.println("Average = "
+ average);
}
}