Java 7: How to execute parallel tasks in batches?
<p>I have three web-service calls that can run in parallel. Hence, I'm using a fixed pool of 3 threads to run them.</p>

<p>Now I want to process a couple more web-service calls, that can run in parallel, but only after the first three calls are processed.</p>

<p>How can I batch them? I want the ones inside a batch to run in parallel. And every batch only runs after the previous batch is completed.</p>

<p>So far I am only working with three services. How can I batch them and start using another 2 services?</p>

<pre><code>  ExecutorService peopleDataTaskExecutor = Executors.newFixedThreadPool(3);

  Future&lt;Collection&lt;PeopleInterface&gt;&gt; task1 = null;
  if (condition) { 
      task1 = peopleDataTaskExecutor.submit(buildTask1Callable(mycontext));
  }

  Future&lt;Map&lt;String, Task2Response&gt;&gt; task2 = peopleDataTaskExecutor.submit(buildTask2Callable(mycontext));

  Future&lt;Map&lt;String, Task3Response&gt;&gt; task3 = null;
  task3 = peopleDataTaskExecutor.submit(buildTask3Callable(mycontext));

  peopleDataTaskExecutor.shutdown();
  try {
      peopleDataTaskExecutor.awaitTermination(10, TimeUnit.SECONDS); 
  } catch (InterruptedException e) {
  }

  Collection&lt;PeopleInterface&gt; task1Data = null;
  try {
      task1Data = task1 != null ? task1.get() : null;
  } catch (InterruptedException | ExecutionException e) {
  }

  Map&lt;String, Task2Response&gt; task2Data = null;
  try {
      task2Data = task2.get();
  } catch (InterruptedException | ExecutionException e) {
  }

  Map&lt;String, Task3Response&gt; task3Data = null;
  if (task3 != null) {
      try {
          task3Data = task3.get();
      } catch (InterruptedException | ExecutionException e) {
      }
  }
</code></pre>