I need help with checking the correctness of my code. I also need help with executing it
Fantastic news! We've Found the answer you've been seeking!
Question:
- I need help with checking the correctness of my code.
- I also need help with executing it into the software Eclipse IDE and a screenshot of the output.
import java.util.Random; class Summation extends Thread { private int[] arr; private int low, high, partial; public Summation(int[] arr, int low, int high) { this.arr = arr; this.low = low; this.high = Math.min(high, arr.length); } public int getPartialSum() { return partial; } public void run() { partial = sum(arr, low, high); } public static int sum(int[] arr) { return sum(arr, 0, arr.length); } public static int sum(int[] arr, int low, int high) { int total = 0; for (int i = low; i < high; i++) { total += arr[i]; } return total; } public static int parallelSum(int[] arr) { return parallelSum(arr, Runtime.getRuntime().availableProcessors()); } public static int parallelSum(int[] arr, int threads) { int size = (int) Math.ceil(arr.length * 1.0 / threads); Summation[] sums = new Summation[threads]; for (int i = 0; i < threads; i++) { sums[i] = new Summation(arr, i * size, (i + 1) * size); sums[i].start(); } try { for (Summation sum : sums) { sum.join(); } } catch (InterruptedException e) { } int total = 0; for (Summation sum : sums) { total += sum.getPartialSum(); } return total; } } public class Main { public static void main(String[] args) { Random rand = new Random(); int[] arr = new int[200000000]; for (int i = 0; i < arr.length; i++) { arr[i] = rand.nextInt(10) + 1; } long start = System.currentTimeMillis(); System.out.println(Summation.sum(arr)); System.out.println("Single: " + (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); System.out.println(Summation.parallelSum(arr)); System.out.println("Parallel: " + (System.currentTimeMillis() - start)); } }
Related Book For
Auditing Cases An Interactive Learning Approach
ISBN: 978-0132423502
4th Edition
Authors: Steven M Glover, Douglas F Prawitt
Posted Date: