title: Asynchronously run all elements in a list (unknown size) of CompletableFutures
body: <p>I am trying to run a list of asynchronous code blocks. I found some help in <a href="https://stackoverflow.com/questions/59108125/return-a-completablefuture-containing-a-list-of-completablefutures">older threads</a>, but the results are inconsistent and show me that not all asynchronous results are completing as expected.</p>
  <p>In this sample code, I prepare a <code>List&lt;CompletableFuture&lt;List&lt;Integer&gt;&gt;&gt;</code>. The list contains a bunch of futures that I want to collect all at once, without waiting one at a time. Each async block just copies the current list of Integers into a new list.</p>
  <pre><code>        List&lt;CompletableFuture&lt;List&lt;Integer&gt;&gt;&gt; futureList = new ArrayList&lt;&gt;();
          List&lt;Integer&gt; ints = new ArrayList&lt;&gt;();
          for (int i=0; i&lt;50; i++) {
              ints.add(Integer.valueOf(i));
              if (ints.size() &lt; 5) {
                  continue;
              }
              System.out.println(String.format(&quot;Input ints: %s&quot;, ints));
              CompletableFuture&lt;List&lt;Integer&gt;&gt; future =
                  CompletableFuture.supplyAsync(
                      () -&gt; {
                          return new ArrayList&lt;&gt;(ints);
                      });
              futureList.add(future);
              ints.clear();
          }
  </code></pre>
  <p>The output is as expected</p>
  <pre><code>Input ints: [0, 1, 2, 3, 4]
  Input ints: [5, 6, 7, 8, 9]
  Input ints: [10, 11, 12, 13, 14]
  Input ints: [15, 16, 17, 18, 19]
  Input ints: [20, 21, 22, 23, 24]
  Input ints: [25, 26, 27, 28, 29]
  Input ints: [30, 31, 32, 33, 34]
  Input ints: [35, 36, 37, 38, 39]
  Input ints: [40, 41, 42, 43, 44]
  Input ints: [45, 46, 47, 48, 49]
  </code></pre>
  <p>Next, I want to collect the results of each future. The code is</p>
  <pre><code>        try {
              CompletableFuture&lt;List&lt;List&lt;Integer&gt;&gt;&gt; allInts = CompletableFuture
                  .allOf(futureList.toArray(new CompletableFuture[0]))
                  .thenApply(v -&gt;
                      futureList.stream().map(CompletableFuture::join).collect(Collectors.toList())
                  );
              System.out.println(String.format(&quot;Output ints: %s&quot;, allInts.get()));
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
  </code></pre>
  <p>The output is inconsistent. Sometimes none of the int lists show up, sometimes some of them show up. Sample results:</p>
  <pre><code>Output ints: [[15, 16, 17, 18, 19], [25, 26, 27, 28, 29], [15, 16, 17, 18, 19], [15, 16, 17, 18, 19], [25, 26, 27, 28, 29], [30, 31, 32, 33, 34], [35, 36, 37, 38, 39], [40, 41, 42, 43, 44], [45, 46, 47, 48, 49], []]
  ------------
  Output ints: [[10, 11, 12, 13, 14], [20, 21, 22, 23, 24], [10, 11, 12, 13, 14], [20, 21, 22], [25, 26, 27, 28, 29], [30, 31, 32, 33, 34], [35, 36, 37, 38, 39], [40, 41, 42, 43, 44], [45, 46, 47, 48, 49], []]
  ------------
  Output ints: [[10, 11, 12, 13, 14], [10, 11, 12, 13, 14], [10, 11, 12, 13, 14], [35, 36, 37, 38, 39], [], [], [], [], [], []]
  </code></pre>
  <p>It's clear that not all of the futures are completing, even though I am running <code>.thenApply()</code> on the <code>.allOf()</code> result. I suspect something is incorrect with the <code>.allOf()</code> input, but I am not sure. Any help would be great.</p>