Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following multithreaded Java program. What should be done to fix this code? A. Nothing needs to be fixed. B. The Sum class should

Consider the following multithreaded Java program. What should be done to fix this code?

image text in transcribed

A. Nothing needs to be fixed.

B. The Sum class should be replaced with a global variable, to simplify the code.

C. The run() method in the thread object "thrd" is never called in this code, it should be called.

D. Both modifications in B and C should be 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 "+upper+" is "+sumobject.getSum()); } catch (InterruptedException ie) { } } else System.err.println("Usage: Summation integer value>"); }

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

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

13-4 What are alternative methods for building information systems?

Answered: 1 week ago