title: Deterministic assignment of tasks to threads using ExecutorService
body: <p>Given Executor service with a fixed pool of threads, is it possible to guarantee deterministic assignment of tasks to threads? More precisely, assume there are just two threads, namely pool-thread-0 and pool-thread-1 and there is a collection of 2 tasks to be executed. What I wish to achieve is that the former thread always executes the first one, while the latter handles the remaining one.</p>
  
  <p>Here is an example:</p>
  
  <pre><code>public static void main(String[] args) throws InterruptedException, ExecutionException {
      ExecutorService executorService = newFixedThreadPool(2,
              new ThreadFactoryBuilder().setNameFormat("pool-thread-%d").build());
  
      for (int i = 0; i &lt; 5; i++) {
          List&lt;Callable&lt;Integer&gt;&gt; callables = ImmutableList.of(createCallable(1), createCallable(2));
          executorService.invokeAll(callables);
  
      }
  }
  
  public static Callable&lt;Integer&gt; createCallable(final int task) {
      return new Callable&lt;Integer&gt;() {
          @Override
          public Integer call() throws Exception {
              currentThread().sleep(1000);
              System.out.println(Thread.currentThread().getName() + " executes task num: " + task);
              return task;
          }
      };
  }
  </code></pre>
  
  <p>Exemplary output from my machine:</p>
  
  <pre><code>pool-thread-0 executes task num: 1
  pool-thread-1 executes task num: 2
  
  pool-thread-0 executes task num: 2
  pool-thread-1 executes task num: 1
  
  pool-thread-0 executes task num: 2
  pool-thread-1 executes task num: 1
  
  pool-thread-0 executes task num: 2
  pool-thread-1 executes task num: 1
  
  pool-thread-0 executes task num: 1
  pool-thread-1 executes task num: 2
  </code></pre>
  
  <p>In a nutshell, I wish to ensure that pool-thread-0 always executes the first task. Any help will be greatly appreciated!</p>