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

  • 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));        }        }              

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Your code appears to be correct for parallel summation using threads Heres a summary of what your co... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!