I have one task which should be done by two thread at the same time.I am going to send one array to both threads to calculate something (For example thread 1 should check half of array and return result and thread two should check the other half and return result) and I want to add the result of both threads as final result in my main
class loop1 extends Thread {
int sum1 = 0;
ArrayList < Integer > list;
public loop1(ArrayList < Integer > lis) {
this.list = list;
}
public void run() {
try {
for (int i = 0; i < list.size() / 2; i++) {
sum1 += lis.get(i);
}
} catch(Exception e) {}
}
return sum1;
}
class loop2 extends Thread {
int sum2 = 0;
ArrayList < Integer > list;
public loop1(ArrayList < Integer > lis) {
this.list = list;
}
public void run() {
try {
for (int i = lis.size() / 2; i < list.size(); i++) {
sum1 += lis.get();
}
} catch(Exception e) {}
}
return sum2;
}
class check {
public static void main(String[] args) {
ArrayList < Integer > list = new ArrayList < >();
loop1 loop1 = new loop1(lis);
loop2 loop2 = new loop2(list);
loop1.start();
loop2.start();
int sum = sum1 + sum2;
System.out.print(sum);
}
}