title: Multiple thenApply in a completableFuture
body: <p>I have a situation where I want to execute some methods in different threads but want to pass the result of one thread to another. I have following methods in my class.</p>
  
  <pre><code>public static int addition(int a, int b){
      System.out.println((a+b));
      return (a+b);
  }
  
  public static int subtract(int a, int b){
      System.out.println((a-b));
      return (a-b);
  }
  
  public static int multiply(int a, int b){
      System.out.println((a*b));
      return (a*b);
  }
  public static String convert(Integer a){
      System.out.println((a));
      return a.toString();
  }
  </code></pre>
  
  <p>here is main method:</p>
  
  <pre><code>public static void main(String[] args) {
      int a = 10;
      int b = 5;
      CompletableFuture&lt;String&gt; cf = new CompletableFuture&lt;&gt;();
      cf.supplyAsync(() -&gt; addition(a, b))
          .thenApply(r -&gt;subtract(20,r)
                  .thenApply(r1 -&gt;multiply(r1, 10))
                  .thenApply(r2 -&gt;convert(r2))
                  .thenApply(finalResult -&gt;{
                      System.out.println(cf.complete(finalResult));
                  }));
      System.out.println(cf.complete("Done"));
  
  }
  </code></pre>
  
  <p>I am trying to pass result of addition to subtraction to multiplication to printing result. But I am getting compilation error. Looks like we can't do nested thenApply(). Is there any way we can do this? Searched it over google and found one helpful link- <a href="http://kennethjorgensen.com/blog/2016/introduction-to-completablefutures" rel="nofollow">http://kennethjorgensen.com/blog/2016/introduction-to-completablefutures</a> But didn't find much help.</p>