title: Java 8 CompletableFuture , Stream and Timeouts
body: <p>i'm trying to process some amount of data concurrently using <code>CompletableFuture</code> and <code>Stream</code>
  So far i have: </p>
  
  <pre><code>public static void main(String[] args) throws InterruptedException, ExecutionException {
      System.out.println("start");
  
      List&lt;String&gt; collect = Stream.of("1", "2", "3", "4", "5",
              "6", "7")
              .map(x -&gt; CompletableFuture.supplyAsync(getStringSupplier(x)))
              .collect(Collectors.toList())
              .stream()
              .map(CompletableFuture::join)
              .collect(Collectors.toList());
      System.out.println("stop out!");
  }
  
  
  public static Supplier&lt;String&gt; getStringSupplier(String text) {
      return () -&gt; {
  
          System.out.println("start " + text);
          try {
              TimeUnit.SECONDS.sleep(2);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          System.out.println("stop " + text);
          return "asd" + text;
      };
  }
  </code></pre>
  
  <p>And output is fine : </p>
  
  <p><code>start
  start 1
  start 4
  start 3
  start 2
  start 5
  start 6
  start 7
  stop 4
  stop 1
  stop 5
  stop 2
  stop 6
  stop 3
  stop 7
  stop out!</code></p>
  
  <p>However right now i want to add timeout to that job. Lets say it should be canceled after 1 SECOND. And return null or some other value to <code>collect</code> list. (I would prefer some value indicating cause). </p>
  
  <p>How can i achieve that ? </p>
  
  <p>Thanks for help in advance.</p>