title: Which executor is used when composing Java CompletableFutures?
body: <p>I have a method on some repository class that returns a <code>CompletableFuture</code>. The code that completes these futures uses a third party library which blocks. I intend to have a separate bounded <code>Executor</code> which this repository class will use to make these blocking calls.</p>
  
  <p>Here is an example:</p>
  
  <pre><code>public class PersonRepository {
      private Executor executor = ...
      public CompletableFuture&lt;Void&gt; create(Person person) {...}
      public CompletableFuture&lt;Boolean&gt; delete(Person person) {...}
  }
  </code></pre>
  
  <p>The rest of my application will compose these futures and do some other things with the results. When these other functions that are supplied to <code>thenAccept</code>, <code>thenCompose</code>, <code>whenComplete</code>, etc, I don't want them to run on the <code>Executor</code> for the repository.</p>
  
  <p>Another example:</p>
  
  <pre><code>public CompletableFuture&lt;?&gt; replacePerson(Person person) {
      final PersonRepository repo = getPersonRepository();
      return repo.delete(person)
          .thenAccept(existed -&gt; {
              if (existed) System.out.println("Person deleted"));
              else System.out.println("Person did not exist"));
          })
          .thenCompose(unused -&gt; {
              System.out.println("Creating person");
              return repo.create(person);
          })
          .whenComplete((unused, ex) -&gt; {
              if (ex != null) System.out.println("Creating person");
              repo.close();
          });
  }
  </code></pre>
  
  <p>The JavaDoc states:</p>
  
  <blockquote>
    <p>Actions supplied for dependent completions of non-async methods may be performed by the thread that completes the current CompletableFuture, or by any other caller of a completion method.</p>
  </blockquote>
  
  <p><strong>Side question:</strong> Why is there an <em>or</em> here? In what case is there another caller of a completion method that does not complete the current future?</p>
  
  <p><strong>Main question:</strong> If I want all the <code>println</code> to be executed by a different <code>Executor</code> than the one used by the repository, which methods do I need to make async and provide the executor manually?</p>
  
  <p>Obviously the <code>thenAccept</code> needs to be changed to <code>thenAcceptAsync</code> but I'm not sure about that point onwards.</p>
  
  <p><strong>Alternative question:</strong> Which thread completes the returned future from <code>thenCompose</code>?</p>
  
  <p>My guess is that it will be whatever thread completes the future returned from the function argument. In other words I would need to also change <code>whenComplete</code> to <code>whenCompleteAsync</code>.</p>
  
  <p>Perhaps I am over complicating things but this feels like it could get quite tricky. I need to pay a lot of attention to where all these futures come from. Also from a design point of view, if I return a future, how do I prevent callers from using my executor? It feels like it breaks encapsulation. I know that all the transformation functions in Scala take an implicit <code>ExecutionContext</code> which seems to solve all these problems.</p>