Correct Implementation of Java Future multithreading
future, java
Solution
You need to submit everything upfront, then wait separately. As below, exception handling removed for clarity:
ExecutorService executor = Executors.newFixedThreadPool(10);
List<Future<statusModel>> futures = new ArrayList<>();
for (Map.Entry<String, String> url : urls.entrySet())
{
futures.add(executor.submit(new CallableRequestStatus(url.getValue())));
}
for (Future<statusModel> f : futures) {
results.add((statusModel) f.get(5, TimeUnit.SECONDS));
}
Problem
In my servlet I am hitting several URL's to check their status and returning the response to user. Hitting multipe requests takes lot of time: need threads and timouts. But i need my threads to geturn response : using Future for that reason. My code outline: ``` ExecutorService executor = Executors.newFixedThreadPool(10); Future<statusModel> future; for (Map.Entry<String, String> url : urls.entrySet()) { try { future = executor.submit(new CallableRequestStatus(url.getValue())); status = (statusModel) future.get(5, TimeUnit.SECONDS); results.add(status); } catch (InterruptedException | ExecutionException | TimeoutException e) { System.out.println("Error<checkServers>: Timeout OR "+e.getMessage()); } } executor.shutdownNow(); ``` All the results from my callable class is coming in status object which I later add to an arraylist. My problem here is that my approach is blocking me from running all 10 threads at same time. I have to wait 5 secs for getting my status object and then move to next URL. I am thinking my approach is faulty. I tried looking online but I couldnt find any example with custom objects and Arraylist involved. Can anyone help me correct my fault. Thanks in advance Finally Updated my code (thanks to Sotirios Delimanolis and Kevin): ``` ExecutorService executor = Executors.newFixedThreadPool(20); List<Future<statusModel>> futures = new ArrayList<Future<statusModel>>(); for (Map.Entry<String, String> url : urls.entrySet()) { Future<statusModel> future = executor.submit(new CallableRequestStatus(url.getValue())); futures.add(future); } ArrayList<statusModel> results = new ArrayList<statusModel>(); statusModel status; int i=0; for (Map.Entry<String, String> url : urls.entrySet()) { try { status = (statusModel) futures.get(i).get(500, TimeUnit.MILLISECONDS); // do some stuff with status and if(status.getStatusCode()/100 == 2) results.add(status); } catch (InterruptedException | ExecutionException | TimeoutException e) { System.out.println("Error<checkServers>: Timeout OR "+e.getMessage()); } i++; } executor.shutdownNow(); System.out.println("Shutdown: "+executor.isShutdown()); ``` Hope its helpful for someone :)