title: Is there a performance benefit of using CompletableFuture callbacks?
body: <p>CompletableFuture allows to provide callbacks for async calls. You can create a long chain of callbacks where each async call will trigger the next one on completion. This is deemed a better way to write async code instead of using Future where you've to block the thread to get the result of first computation before triggering the next one. </p>
  
  <p>I can understand the argument that callback chains in Completable Futures can provide a more readable code but I'm wondering if there's a performance benefit as well to this approach or is it just a syntactic sugar?</p>
  
  <p>For example, consider the following code:</p>
  
  <pre class="lang-java prettyprint-override"><code>    ExecutorService exec = Executors.newSingleThreadExecutor();
      CompletableFuture.supplyAsync(this::findAccountNumber, exec)
                       .thenApply(this::calculateBalance)
                       .thenApply(this::notifyBalance)
                       .thenAccept((i)-&gt;notifyByEmail())
                       .join();
  </code></pre>
  
  <p>In this code, <code>calculateBalance()</code> can't start until <code>findAccountNumber()</code> finishes so essentially <code>calculateBalance()</code> is blocked on <code>findAccountNumber()</code> and so on for the next methods in the callback chain. How is it better than the following (performance-wise):</p>
  
  <pre><code>    ExecutorService exec = Executors.newSingleThreadExecutor();
      Future&lt;Integer&gt; accountNumberFuture = exec.submit(findAccountNumberCallable);
      Integer accountNumber = accountNumberFuture.get();
      Future&lt;String&gt; calculateBalanceFuture = exec.submit(calculateBalanceCallable(accountNumber);
      ....
      ....
  
  </code></pre>