Reputation: 49
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);
}
}
Upvotes: 0
Views: 84
Reputation: 18245
loop1.start();
loop2.start();
// here you should check that both loop1 and loop2 thread has been finished
int sum = loop1.sum1 + loop2.sum2;
public class Check {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService pool = Executors.newFixedThreadPool(2);
int[] arr = createArray(100);
Future<Integer> task1 = calculate(arr, 0, arr.length / 2, pool);
Future<Integer> task2 = calculate(arr, arr.length / 2, arr.length, pool);
while (!task1.isDone() || !task2.isDone()) {
Thread.sleep(300);
}
System.out.println(task1.get() + task2.get());
}
private static int[] createArray(int length) {
int[] arr = new int[length];
for (int i = 0; i < arr.length; i++)
arr[i] = i;
return arr;
}
private static Future<Integer> calculate(final int[] arr, final int fromInclusive, final int toExclusive, ExecutorService pool) {
return pool.submit(() -> {
int sum = 0;
for (int i = fromInclusive; i < toExclusive; i++)
sum += arr[i];
return sum;
});
}
}
public class Check {
public static void main(String[] args) throws InterruptedException, ExecutionException {
int[] arr = createArray(100);
Task task1 = new Task(arr, 0, arr.length / 2);
Task task2 = new Task(arr, arr.length / 2, arr.length);
task1.start();
task2.start();
task1.join();
task2.join();
System.out.println(task1.getSum() + task2.getSum());
}
private static int[] createArray(int length) {
int[] arr = new int[length];
for (int i = 0; i < arr.length; i++)
arr[i] = i;
return arr;
}
private static final class Task extends Thread {
private final int[] arr;
private final int fromInclusive;
private final int toExclusive;
private int sum;
public Task(int[] arr, int fromInclusive, int toExclusive) {
this.arr = arr;
this.fromInclusive = fromInclusive;
this.toExclusive = toExclusive;
}
public int getSum() {
return sum;
}
@Override
public void run() {
for (int i = fromInclusive; i < toExclusive; i++)
sum += arr[i];
}
}
}
Upvotes: 1