Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given the JAVA code below, 1. Run the code and use it to calculate sum from 1-N, ( try N =100000000). 2. Measure the time
Given the JAVA code below, 1. Run the code and use it to calculate sum from 1-N, ( try N =100000000). 2. Measure the time taken of the programs to calculate this sum. And print it. 3. Modify the code to have two threads, one will calculate sum from 1 to N/2 and the second will Calculate sum from N/2 +1 to N. In the main method, add the two partial sums and display the result. 4. Measure the time taken of the new program and compare it to what you had in 2.
Given the JAVA code below, 1. Run the code and use it to calculate sum from 1-N, (try N=100000000). 2. Measure the time taken of the programs to calculate this sum. And print it. 3. Modify the code to have two threads, one will calculate sum from 1 to N/2 and the second will Calculate sum from N/2 +1 to N. In the main method, add the two partial sums and display the result 4. Measure the time taken of the new program and compare it to what you had in 2. You may use any Java library to help you get it done. class Sum { private int sum; public int getSum() { return sum; } public void set Sum(int sum) { this.sum = sum; } } class Summation implements Runnable { private int upper; private Sum sumValue; public Summation (int upper, Sum sumValue) { this.upper = upper; this.sumvalue sumValue; } public void run() { int sum = 0; for (int i = 0; i 0) { if (Integer.parseInt (args[0]) = 0."); else { Sum sumobject = new Sum(); int upper = Integer.parseInt(args[0]); Thread thrd = new Thread (new Summation (upper, sumobject)); thrd.start(); try { thrd.join(); System.out.println ("The sum of "+uppert" is " +sumObject.getSum()); } catch (InterruptedException ie) {} } } else System.err.println("Usage: SummationStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started