File tree Expand file tree Collapse file tree 4 files changed +56
-0
lines changed
4-Multithreaded programming/java Expand file tree Collapse file tree 4 files changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .io .*;
2+
3+ class Sum
4+ {
5+ private int sum ;
6+
7+ public int getSum (){
8+ return sum ;
9+ }
10+
11+ public void setSum (int sum ){
12+ this .sum = sum ;
13+ }
14+ }
15+
16+ class Summation implements Runnable
17+ {
18+ private int upper ;
19+ private Sum sumValue ;
20+
21+ public Summation (int upper , Sum sumValue ){
22+ this .upper = upper ;
23+ this .sumValue = sumValue ;
24+ }
25+
26+ public void run (){
27+ int sum = 0 ;
28+ for (int i =0 ; i <=upper ; i ++){
29+ sum += i ;
30+ }
31+ sumValue .setSum (sum );
32+ }
33+ }
34+
35+ public class summation
36+ {
37+ public static void main (String [] args ){
38+ if (args .length > 0 ){
39+ if (Integer .parseInt (args [0 ]) < 0 )
40+ System .err .println (args [0 ] + " must be >= 0." );
41+ else {
42+ Sum sumObject = new Sum ();
43+ int upper = Integer .parseInt (args [0 ]);
44+ Thread thread = new Thread (new Summation (upper , sumObject ));
45+ thread .start ();
46+ try {
47+ thread .join ();
48+ System .out .println ("sum = " + sumObject .getSum ());
49+ }catch (InterruptedException ie ){
50+ System .err .println (ie );
51+ }
52+ }
53+ }
54+ else System .err .println ("Usage: summation <integer value>" );
55+ }
56+ }
You can’t perform that action at this time.
0 commit comments