-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathAddThreadSample.java
More file actions
51 lines (36 loc) · 851 Bytes
/
AddThreadSample.java
File metadata and controls
51 lines (36 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class AddThreadSample {
public static void main(String[] args) throws InterruptedException {
int[] arr = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
AddThread t1 = new AddThread(arr , 0 , 3);
AddThread t2 = new AddThread(arr , 4 , 7);
AddThread t3 = new AddThread(arr , 8 , 11);
AddThread t4 = new AddThread(arr , 12 , 15);
t1.start();
t2.start();
t3.start();
t4.start();
t1.join();
t2.join();
t3.join();
t4.join();
int total = t1.sum + t2.sum + t3.sum + t4.sum;
System.out.println(total);
}
}
class AddThread extends Thread{
int[] arr;
int start;
int end;
int sum;
public AddThread(int[] arr, int start, int end) {
super();
this.arr = arr;
this.start = start;
this.end = end;
}
public void run() {
for(int i = start ; i <= end ; i++ ) {
sum += arr[i];
}
}
}