Get Results from Parallel in Java Thread


Create package demo and create new java class as below:

package thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ParallelTasksReturnResults {

	private static ExecutorService threadLauncher = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

	private List<Callable<Object>> callableList = new ArrayList<Callable<Object>>();

	public void addCallable(Callable<Object> callable) {
		this.callableList.add(callable);
	}

	public void clearCallables() {
		this.callableList.clear();
	}

	public void executeThreads() {
		try {
			threadLauncher.invokeAll(this.callableList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Object[] getResult() {
		List<Future<Object>> resultList = null;
		Object[] resultArray = null;
		try {
			resultList = threadLauncher.invokeAll(this.callableList);
			resultArray = new Object[resultList.size()];
			for (int i = 0; i < resultList.size(); i++) {
				resultArray[i] = resultList.get(i).get();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultArray;
	}

}




In demo package, create Main java class as below:

package demo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

public class Main {

	public static void main(String[] args) {

		try {
			List<Integer> a = new ArrayList<Integer>();
			a.add(5);
			a.add(-2);
			a.add(11);
			a.add(20);
			a.add(-3);
			a.add(9);
			a.add(-4);

			ParallelTasksReturnResults parallelTasksReturnResults = new ParallelTasksReturnResults();

			Callable<Object> countPositiveNumbers = new Callable<Object>() {
				@Override
				public Integer call() {
					int count = 0;
					for (int i = 0; i < a.size(); i++) {
						if (a.get(i) > 0) {
							count++;
						}
					}
					return count;
				}
			};

			Callable<Object> countNegativeNumbers = new Callable<Object>() {
				@Override
				public Integer call() {
					int count = 0;
					for (int i = 0; i < a.size(); i++) {
						if (a.get(i) < 0) {
							count++;
						}
					}
					return count;
				}
			};

			Callable<Object> countEvenNumbers = new Callable<Object>() {
				@Override
				public Integer call() {
					int count = 0;
					for (int i = 0; i < a.size(); i++) {
						if (a.get(i) % 2 == 0) {
							count++;
						}
					}
					return count;
				}
			};

			Callable<Object> countOddNumbers = new Callable<Object>() {
				@Override
				public Integer call() {
					int count = 0;
					for (int i = 0; i < a.size(); i++) {
						if (a.get(i) % 2 != 0) {
							count++;
						}
					}
					return count;
				}
			};

			parallelTasksReturnResults.addCallable(countEvenNumbers);
			parallelTasksReturnResults.addCallable(countNegativeNumbers);
			parallelTasksReturnResults.addCallable(countOddNumbers);
			parallelTasksReturnResults.addCallable(countPositiveNumbers);

			Object[] objects = parallelTasksReturnResults.getResult();
			for (Object object : objects) {
				System.out.println(object);
			}

		} catch (Exception e) {
			System.err.println(e.getMessage());
		}

	}

}




3
3
4
4