import java.util.ArrayList; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class ESMain { public static void main(String[] args){ int nrOfThreads = Runtime.getRuntime().availableProcessors(); ExecutorService executorService = Executors.newFixedThreadPool(nrOfThreads); //class MyTask extends Callable //create and submit tasks ArrayList> futures = new ArrayList<>(); for(int i=0; i<10; i++){ MyTask myTask = new MyTask(); Future future = executorService.submit(myTask); futures.add(future); } //collect the results Integer globalRsult = 0; for(Future future : futures){ try { Integer localResult = future.get(); globalRsult += localResult; } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } System.out.println("Global result " + globalRsult); executorService.shutdownNow(); } }