Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

Your code appears to be correct for parallel summation using threads Heres a summary of what your co... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Auditing Cases An Interactive Learning Approach

Authors: Steven M Glover, Douglas F Prawitt

4th Edition

0132423502, 978-0132423502

More Books

Students also viewed these Programming questions

Question

How do we do subnetting in IPv6?Explain with a suitable example.

Answered: 1 week ago

Question

Explain the guideline for job description.

Answered: 1 week ago

Question

What is job description ? State the uses of job description.

Answered: 1 week ago

Question

What are the objectives of job evaluation ?

Answered: 1 week ago

Question

Write a note on job design.

Answered: 1 week ago