Calling a method in parallel which returns the future I have an async method which calls my Task class while my Task class does all the work:

    private ExecutorService executor = Executors.newFixedThreadPool(10);

    @Override
    public Future&lt;DataResponse&gt; executeAsync(DataKey key) {
        Future&lt;DataResponse&gt; future = null;

        try {
            Task task = new Task(key, restTemplate);
            future = executor.submit(task); 
        } catch (Exception ex) {
            // logging exception here
        }

        return future;
    }


Here is my Task class which does all the work:

public class Task implements Callable&lt;DataResponse&gt; {

    private DataKey key;
    private RestTemplate restTemplate;

    public Task(DataKey key, RestTemplate restTemplate) {
        this.key = key;
        this.restTemplate = restTemplate;
    }

    @Override
    public DataResponse call() throws Exception {
        // some code here
    }
}


Now I need to call executeAsync method in parallel and then make a List&lt;DataResponse&gt; object and return it:

@Override
public List&lt;DataResponse&gt; executeSync(DataKey key) {
    List&lt;DataResponse&gt; responseList = new ArrayList&lt;DataResponse&gt;();

    // make a List of DataKey using single key passed to this method.       
    List&lt;DataKey&gt; keys = new ArrayList&lt;DataKey&gt;();

    // here keys max size will be three
    List&lt;Future&gt; futures = new ArrayList&lt;&gt;(keys.size());
    for(DataKey key : keys) {
        Future&lt;DataResponse&gt; future = executeAsync(key);
        futures.add(future);
    }

    for (Future&lt;DataResponse&gt; future : futures) {
        try {
            responseList.add(future.get(300, TimeUnit.MILLISECONDS));
        } catch (Exception e) {
            // do something
        }
    }
    return responseList;
}


My intention is to call executeAsync method in parallel and return back responseList object. I mean I want to execute call method of my Task class in parallel for each of the key object so if I have three key object in keys list, then I want to execute call method for each key in parallel and then make a List&lt;DataResponse&gt; object and return it.

Since I don't want to wait 300 Milliseconds for each key future and then call next key future and then next call third key future. In this way it will wait 300 + 300 + 300 for each of the key. I want to execute call method in parallel in same 300 milliseconds for all three keys.

Does my above code looks right, and are there any better ways of doing this problem? I am still on Java 7.
An executor service will be able to do a invokeAll with a timeout and return a set of futures holding all the values or return when the timeout expires.

This means that your executeAsync should take a set of keys and build all the tasks from those at once and then submit the tasks as a batch:

@Override
public List&lt;Future&lt;DataResponse&gt;&gt; executeAsync(Collection&lt;DataKey&gt; keys,  long timeout, TimeUnit unit) {
    List&lt;Task&gt; tasks = new ArrayList&lt;&gt;(keys.size());
    for(DataKey key : keys){
        tasks.add(new Task(key, restTemplate));
    }

    return executor.invokeAll(tasks, timeout, unit);
}


@Override
public List&lt;DataResponse&gt; executeSync(DataKey key) {
    List&lt;DataResponse&gt; responseList = new ArrayList&lt;DataResponse&gt;();

    // make a List of DataKey using single key passed to this method.       
    List&lt;DataKey&gt; keys = new ArrayList&lt;DataKey&gt;();

    List&lt;Future&gt; futures = executeAsync(keys, 300, TimeUnit.MILLISECONDS);


    for (Future&lt;DataResponse&gt; future : futures) {
        try {
            if(future.isDone())
                responseList.add(future.get());
            //otherwise future didn't complete in the 300 milliseconds
        } catch (Exception e) {
            // do something
        }
    }
    return responseList;
}