title: completionservice: how to kill all threads and return result through 5 seconds?
body: <p>I have some problem with CompletionService.
  My task: to parse parallely for about 300 html pages,  <strong>i need to wait for all the results only for 5 seconds,</strong> then - return the result to main code.
  I`ve decided to use CompletionService + Callable for this.
  <strong>The question is how to stop all threads, that were caused by CompletionService and return the result from that pages, that were successfully parsed?</strong> In this code removed printlines, but i can say that 5 seconds is enough(there are good results, but program wait when all threads will be completed). My code performed about 2 minutes.</p>
  
  <p><strong>My calling code:</strong></p>
  
  <pre><code>Collection&lt;Callable&lt;HCard&gt;&gt; solvers = new ArrayList&lt;Callable&lt;HCard&gt;&gt;();
  for (final String currentUrl : allUrls) {
      solvers.add(new Callable&lt;HCard&gt;() {
          public HCard call() throws ParserException {
              HCard hCard = HCardParser.parseOne(currentUrl);                      
              if (hCard != null) {
                  return hCard;
              } else {
                  return null;
              }
          }
      });
  }
  ExecutorService execService = Executors.newCachedThreadPool();
  Helper helper = new Helper();
  List&lt;HCard&gt; result = helper.solve(execService, solvers);
  //then i do smth with result list
  </code></pre>
  
  <p><strong>My called code:</strong></p>
  
  <pre><code>public class Helper {
  List&lt;HCard&gt; solve(Executor e, Collection&lt;Callable&lt;HCard&gt;&gt; solvers) throws InterruptedException {
      CompletionService&lt;HCard&gt; cs = new ExecutorCompletionService&lt;HCard&gt;(e);
      int n = solvers.size();
  
      Future&lt;HCard&gt; future = null;
      HCard hCard = null;
      ArrayList&lt;HCard&gt; result = new ArrayList&lt;HCard&gt;();
  
      for (Callable&lt;HCard&gt; s : solvers) {
          cs.submit(s);
      }
      for (int i = 0; i &lt; n; ++i) {
          try {
              future = cs.take();
              hCard = future.get();
              if (hCard != null) {
                  result.add(hCard);
              }
          } catch (ExecutionException e1) {
              future.cancel(true);
          }
      }
      return result;
  }
  </code></pre>
  
  <p>I attempted to use: </p>
  
  <ul>
  <li>awaitTermination(5000, TimeUnit.MILLISECONDS)      </li>
  <li>future.cancel(true)     </li>
  <li>execService.shutdownNow()    </li>
  <li>future.get(5000, TimeUnit.MILLISECONDS);</li>
  <li>TimeOutException: i can`t get TimeOutException.</li>
  </ul>
  
  <p>Please, help me on context of my code.<br>
  Thanks in advance! </p>