Answered step by step
Verified Expert Solution
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?
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
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