Answered step by step
Verified Expert Solution
Link Copied!

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.image text in transcribedimage text in transcribedimage text in transcribed

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: Summation "); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions

Question

What is American Polity and Governance ?

Answered: 1 week ago

Question

What is Constitution, Political System and Public Policy? In India

Answered: 1 week ago

Question

What is Environment and Ecology? Explain with examples

Answered: 1 week ago