Skip to content

Commit d24d13d

Browse files
committed
Using java threads
1 parent 3df5dd9 commit d24d13d

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed
336 Bytes
Binary file not shown.
481 Bytes
Binary file not shown.
1.23 KB
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

0 commit comments

Comments
 (0)