title: Callable lambda expression with argument
body: <p>I am trying to implement "TaskExecutor" with generics (my first attempt to use generics) and using ExecutorService.</p>
  
  <p>Here is my "TaskExecutor" class:</p>
  
  <pre><code>public class ExecuteAlerterTask&lt;T&gt; {
      public List&lt;T&gt; process(String executorName, Callable&lt;T&gt; task) throws ExecutionException, InterruptedException {
          final ThreadFactory threadFactory = new ThreadFactoryBuilder()
                  .setNameFormat(executorName + "-%d")
                  .setDaemon(true)
                  .build();
          ExecutorService executor = Executors.newFixedThreadPool(10, threadFactory);
          Collection&lt;Future&lt;T&gt;&gt; futures = new ArrayList&lt;&gt;();
          IntStream.range(1, 10).forEach(i -&gt; {
              Future&lt;T&gt; future = executor.submit(task);
              futures.add(future);
          });
  
          List&lt;T&gt; result = new ArrayList&lt;&gt;();
          for (Future&lt;T&gt; f : futures) {
              result.add(f.get());
          }
          executor.shutdown();
          return result;
      }
  }
  </code></pre>
  
  <p>Here is my way to run it:</p>
  
  <pre><code>@Test
  public void process() throws Exception {
      Callable&lt;String&gt; callable = () -&gt; "Do something on ";
      ExecuteAlerterTask&lt;String&gt; executeAlerterTask = new ExecuteAlerterTask&lt;&gt;();
      List&lt;String&gt; result = executeAlerterTask.process("TaskName", callable);
      result.forEach(System.out::println);
  }
  </code></pre>
  
  <p>And here is my <strong>question</strong>:
  How to write my Callable that it would accept argument <strong>i</strong> at a line:</p>
  
  <pre><code>Future&lt;T&gt; future = executor.submit(task);
  </code></pre>
  
  <p>E.g. desired <strong>result</strong> would be:</p>
  
  <pre><code>Do something on 1
  Do something on 3
  Do something on 7
  Do something on 2
  &lt;...etc...&gt;
  </code></pre>
  
  <p>If something else is wrong with my code - please let me know.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Removed implements Callable</p>
  
  <p>Code above is <strong>abstraction</strong> of what I really want to do:</p>
  
  <ul>
  <li>IntRange really is set of batches where I get data from SQL. Callable
  really implements logic how to process those SQL batches.</li>
  </ul>
  
  <p><strong>EDIT2</strong></p>
  
  <p>After all suggestions I have following solution:</p>
  
  <pre><code>public class ExecuteAlerterTask&lt;T&gt; {
      public List&lt;T&gt; process(String executorName, Collection&lt;Callable&lt;T&gt;&gt; task) throws ExecutionException, InterruptedException {
          final ThreadFactory threadFactory = new ThreadFactoryBuilder()
                  .setNameFormat(executorName + "-%d")
                  .setDaemon(true)
                  .build();
          ExecutorService executor = Executors.newFixedThreadPool(10, threadFactory);
          Collection&lt;Future&lt;T&gt;&gt; futures = executor.invokeAll(task);
  
          List&lt;T&gt; result = new ArrayList&lt;&gt;();
          for (Future&lt;T&gt; f : futures) {
              result.add(f.get());
          }
          executor.shutdown();
          return result;
      }
  }
  </code></pre>
  
  <p>And way to run it:</p>
  
  <pre><code>   @Test
      public void process() throws Exception {
          Collection&lt;Callable&lt;String&gt;&gt; tasks = new ArrayList&lt;&gt;();
          IntStream.range(1, 10).forEach(i -&gt; {
              tasks.add(new Task(i).callable);
          });
  
          ExecuteAlerterTask&lt;String&gt; executeAlerterTask = new ExecuteAlerterTask&lt;&gt;();
          List&lt;String&gt; result = executeAlerterTask.process("TaskName", tasks);
          result.forEach(System.out::println);
      }
  
      private class Task {
          private int i;
          private Callable&lt;String&gt; callable = () -&gt; "Doing something on i: " + i;
          private Task(int i) {
              this.i = i;
          }
      }
  </code></pre>
  
  <p><strong>EDIT3</strong></p>
  
  <p>Even simpler way to run it:</p>
  
  <pre><code>   @Test
      public void process() throws Exception {
          Collection&lt;Callable&lt;String&gt;&gt; tasks = new ArrayList&lt;&gt;();
          IntStream.range(1, 10).forEach(i -&gt; {
              tasks.add(() -&gt; "Do something on i: " + i * 2);
          });
  
          ExecuteAlerterTask&lt;String&gt; executeAlerterTask = new ExecuteAlerterTask&lt;&gt;();
          List&lt;String&gt; result = executeAlerterTask.process("TaskName", tasks);
          result.forEach(System.out::println);
      }
  </code></pre>
  
  <p>I guess I quite happy with final solution. Thanks all!</p>