-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentSum.java
More file actions
102 lines (93 loc) · 3.74 KB
/
ConcurrentSum.java
File metadata and controls
102 lines (93 loc) · 3.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
package module7.parallelism.sum;
public class ConcurrentSum {
public static long getLinearSum(int[] arr) {
long sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static long getLinearSum(short[] arr) {
long sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
/**
* Calculate a sum of the integers in an array, using multithreading.
*
* @implNote This implementation uses four threads, and as such, is best optimized for quad-core processers.
* @param arr An array of integers
* @return The sum of the numbers in the array
*/
public static long getConcurrentSum(int[] arr) {
long[] results = new long[3];
int sliceLength = (int)Math.ceil(arr.length / 3);
// Create threads, each of which sums one quarter of the array.
Thread t1 = new Thread(() -> results[0] = getPartialSum(arr, sliceLength * 0, sliceLength));
Thread t2 = new Thread(() -> results[1] = getPartialSum(arr, sliceLength * 1, sliceLength));
Thread t3 = new Thread(() -> results[2] = getPartialSum(arr, sliceLength * 2, sliceLength * 2));
// Start the threads
t1.start(); t2.start(); t3.start();
try {
// Wait for all threads to finish
t1.join(); t2.join(); t3.join();
} catch (InterruptedException e) {
System.err.println(e.getMessage());
}
return results[0] + results[1] + results[2];
}
public static long getConcurrentSum(short[] arr) {
long[] results = new long[3];
int sliceLength = (int) Math.ceil(arr.length / 3);
// Create threads, each of which sums one quarter of the array.
Thread t1 = new Thread(() -> results[0] = getPartialSum(arr, sliceLength * 0, sliceLength));
Thread t2 = new Thread(() -> results[1] = getPartialSum(arr, sliceLength * 1, sliceLength));
Thread t3 = new Thread(() -> results[2] = getPartialSum(arr, sliceLength * 2, sliceLength + 1));
// Start the threads
t1.start();
t2.start();
t3.start();
try {
// Wait for all threads to finish
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {
System.err.println(e.getMessage());
}
return results[0] + results[1] + results[2];
}
/**
* Calculates the sum of all the elements within a segment of an array.
* The search ends after the element with index {@code startIndex + sliceLength - 1}
* @param arr An array of integers to search
* @param startIndex The index to begin the count from
* @param sliceLength The number of elements to include.
*/
private static long getPartialSum(int[] arr, int startIndex, int sliceLength) {
long sum = 0;
int endIndex = startIndex + sliceLength;
for (int i = startIndex; i < endIndex && i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
/**
* Calculates the sum of all the elements within a segment of an array. The
* search ends after the element with index {@code startIndex + sliceLength - 1}
*
* @param arr An array of integers to search
* @param startIndex The index to begin the count from
* @param sliceLength The number of elements to include.
*/
private static long getPartialSum(short[] arr, int startIndex, int sliceLength) {
long sum = 0;
int endIndex = startIndex + sliceLength;
for (int i = startIndex; i < endIndex && i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
}