Question: Show the output of the java program Write the output of the following multithreading programming in Java. class Sum { private int sum; public int
Show the output of the java program
Write the output of the following multithreading programming in Java.
class Sum
{ private int sum;
public int getSum()
{ return sum;
}
public void setSum(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<=upper;i++)
sum+=i;
sumValue.setSum(sum);
}
}
public class TestJavaThread
{ public static void main(String[] args)
{
int upper = 10;
/* create the object which will be shared by the thread */
Sum sumObject=new Sum();
/* create a thread object */
Thread thrd=new Thread(new Summation(upper,sumObject));
/* Create and use thread */
thrd.start();
try
{ thrd.join();
System.out.println("the sum from 1 to " + upper +" is " +sumObject.getSum());
} catch (InterruptedException ie) {}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
