title: Optimal orchestration of list of network calls and processing tasks in Java
body: <p>I have the following workflow:
  There are <code>n</code> records that need to be retrieved over the network and subsequently <code>n</code> expensive computations that need to be done on each. Put in code, this would look like:</p>
  
  <pre><code>List&lt;Integer&gt; ids = {1,2,....n};
  ids.forEach(id -&gt; {
      Record r = RetrieveRecord(id); // Blocking IO
      ProcessRecord(r); // CPU Intensive
  })
  </code></pre>
  
  <p>I would like to convert the blocking part into async so that the time is minimized with a single thread- essentially, by ensuring that record <code>i+1</code> is being retrieved when record <code>i</code> is being processed. So that the execution would look like:</p>
  
  <pre><code>Retrieve(1).start()
  Retrieve(1).onEnd(() -&gt; { start Retrieve(2), Process(1) })
  Retrieve(2).onEnd(() -&gt; { start Retrieve(3), Process(2) })
  ....
  </code></pre>
  
  <p>Now I can come up with the naive way to implement this with a <code>List&lt;&gt;</code> and <code>CompletableFuture</code>, but this would require me to handle the first record differently.</p>
  
  <p>Is there a more elegant way of solving this with something like reactive streams? 
  A solution that would maybe let me easily configure how many records <code>Process()</code> can trail behind <code>Retreive()</code>?</p>