title: Get result immediately when task completed in ThreadPool
body: <p>I refer to <a href="http://java.dzone.com/articles/java-concurrency-%E2%80%93-part-7" rel="nofollow noreferrer">this link</a> to create a fixed size threadpool. Then I have a method which allow submit <code>Callable</code> request and get the result, it look like this:</p>
  <pre><code> private ExecutorService threadPool = Executors.newFixedThreadPool(5);
  private CompletionService&lt;String&gt; pool = new ExecutorCompletionService&lt;String&gt;(threadPool);
  
   public void execute(Callable&lt;String&gt; request){
      pool.submit(request);
      // what happen if this method is called before get the result???
      try {
          String result = pool.take().get();
          System.out.println(&quot;result is &quot; + result);
      } catch (InterruptedException e) {
          e.printStackTrace();
      } catch (ExecutionException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  <p>This execute method can be called many times and the request has difference  execute time. The problem is that I want to <strong>get the result immediately</strong> when it finished. And I want to <strong>make sure when executing this method, other calls can be handled and allow add to thread poll</strong>.<br>
  Here is an example usage:</p>
  <pre><code>final Random rnd = new Random();
  for (int i = 0; i &lt; 5; i++) {
          final String value = String.valueOf(i);
          execute(new Callable&lt;String&gt;() {
              
              @Override
              public String call() throws Exception {
                  int sleep = rnd.nextInt(10) * 100;
                  System.out.println(&quot;sleep in &quot; + sleep);
                  Thread.sleep(sleep);
                  return value;
              }
          });
      }
  </code></pre>
  <p>And the results are always in order although they have difference execute time:</p>
  <pre><code>  sleep in 900
    result is 0
    sleep in 300
    result is 1
    sleep in 0
    result is 2
    sleep in 500
    result is 3
    sleep in 600
    result is 4
  </code></pre>
  <p>And I also used the <code>future</code>, but it doesn't work too.</p>
  <pre><code> private static void execute(Callable&lt;String&gt; request){
   Future&lt;String&gt; future = threadPool.submit(request);
      try {
          String result = future.get();
          System.out.println(&quot;result is &quot; + result);
      } catch (InterruptedException e) {
          e.printStackTrace();
      } catch (ExecutionException e) {
          e.printStackTrace();
      }
      }
  </code></pre>
  <p>Please tell me how can I do that? Thanks in advance.</p>