|
| 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 | +} |
0 commit comments