Skip to content

Commit 0a01e75

Browse files
committed
(Day 13) Completed ParallelArrayInitializer and ParallelSumTask Excercises
1 parent 0cef6be commit 0a01e75

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.util.concurrent.RecursiveAction;
2+
import java.util.concurrent.ForkJoinPool;
3+
import java.util.Random;
4+
5+
/**
6+
*
7+
* *30.12 (Parallel array initializer) Implement the following method using the Fork/
8+
* Join Framework to assign random values to the list.
9+
*
10+
* public static void parallelAssignValues(double[] list)
11+
* Write a test program that creates a list with 9,000,000 elements and invokes
12+
* parallelAssignValues to assign random values to the list. Also
13+
* implement a sequential algorithm and compare the execution time of the
14+
* two. Note that if you use Math.random(), your parallel code execution time will be worse than the sequential code execution time because
15+
* Math.random() is synchronized and cannot be executed in parallel. To
16+
* fix this problem, create a Random object for assigning random values to a
17+
* small list.
18+
*
19+
*/
20+
21+
public class ParallelArrayInitializer extends RecursiveAction {
22+
private static final int THRESHOLD = 100000;
23+
private double[] list;
24+
private int start;
25+
private int end;
26+
private Random random = new Random(); // Create a Random object for generating random values
27+
28+
public ParallelArrayInitializer(double[] list, int start, int end) {
29+
this.list = list;
30+
this.start = start;
31+
this.end = end;
32+
}
33+
34+
@Override
35+
protected void compute() {
36+
if (end - start <= THRESHOLD) {
37+
for (int i = start; i < end; i++) {
38+
list[i] = random.nextDouble(); // Assign random value using the Random object
39+
}
40+
} else {
41+
int mid = start + (end - start) / 2;
42+
ParallelArrayInitializer leftTask = new ParallelArrayInitializer(list, start, mid);
43+
ParallelArrayInitializer rightTask = new ParallelArrayInitializer(list, mid, end);
44+
45+
invokeAll(leftTask, rightTask);
46+
}
47+
}
48+
49+
public static void parallelAssignValues(double[] list) {
50+
ForkJoinPool pool = new ForkJoinPool();
51+
pool.invoke(new ParallelArrayInitializer(list, 0, list.length));
52+
}
53+
54+
public static void sequentialAssignValues(double[] list) {
55+
Random random = new Random();
56+
for (int i = 0; i < list.length; i++) {
57+
list[i] = random.nextDouble();
58+
}
59+
}
60+
61+
public static void main(String[] args) {
62+
int size = 9000000;
63+
double[] list1 = new double[size];
64+
double[] list2 = new double[size];
65+
66+
long startTime = System.currentTimeMillis();
67+
parallelAssignValues(list1);
68+
long endTime = System.currentTimeMillis();
69+
System.out.println("Parallel assign time: " + (endTime - startTime) + "ms");
70+
71+
startTime = System.currentTimeMillis();
72+
sequentialAssignValues(list2);
73+
endTime = System.currentTimeMillis();
74+
System.out.println("Sequential assign time: " + (endTime - startTime) + "ms");
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.util.concurrent.RecursiveTask;
2+
import java.util.concurrent.ForkJoinPool;
3+
4+
/**
5+
*
6+
* *30.15 (Parallel sum) Implement the following method using Fork/Join to find the sum of a list.
7+
8+
* public static double parallelSum(double[] list)
9+
* Write a test program that finds the sum in a list of 9,000,000 double values.
10+
*
11+
*/
12+
13+
public class ParallelSumTask extends RecursiveTask<Double> {
14+
private static final int THRESHOLD = 100000; // Adjust this threshold based on your needs
15+
private double[] list;
16+
private int start;
17+
private int end;
18+
19+
public ParallelSumTask(double[] list, int start, int end) {
20+
this.list = list;
21+
this.start = start;
22+
this.end = end;
23+
}
24+
25+
@Override
26+
protected Double compute() {
27+
if (end - start <= THRESHOLD) {
28+
double sum = 0;
29+
for (int i = start; i < end; i++) {
30+
sum += list[i];
31+
}
32+
return sum;
33+
} else {
34+
int mid = start + (end - start) / 2;
35+
ParallelSumTask leftTask = new ParallelSumTask(list, start, mid);
36+
ParallelSumTask rightTask = new ParallelSumTask(list, mid, end);
37+
38+
leftTask.fork();
39+
double rightResult = rightTask.compute();
40+
double leftResult = leftTask.join();
41+
42+
return leftResult + rightResult;
43+
}
44+
}
45+
46+
public static double parallelSum(double[] list) {
47+
ForkJoinPool pool = new ForkJoinPool();
48+
return pool.invoke(new ParallelSumTask(list, 0, list.length));
49+
}
50+
51+
public static void main(String[] args) {
52+
int size = 9000000;
53+
double[] list = new double[size];
54+
for (int i = 0; i < size; i++) {
55+
list[i] = Math.random(); // Filling the array with random double values
56+
}
57+
58+
long startTime = System.currentTimeMillis();
59+
double sum = parallelSum(list);
60+
long endTime = System.currentTimeMillis();
61+
62+
System.out.println("Parallel sum: " + sum);
63+
System.out.println("Time taken: " + (endTime - startTime) + "ms");
64+
}
65+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Java Fundamentals
22

3-
*A summary of all the most important chapters in **Introduction to Java Programing - Y.Daniel.Liang***
3+
\*A summary of all the most important chapters in **Introduction to Java Programing - Y.Daniel.Liang\***
44

55
## What is this repository ?
66

0 commit comments

Comments
 (0)