title: How to use Future and Callable in recursive method?
body: <p>I am trying to use Future and Callable to turn a dfs recursive method into parallel to improve efficiency. But not sure how to do it right. Here is the code:</p>
  
  <pre><code>public MyResult compute(MyTree tree, int depth, ExecutorService exec) {
      if (depth &gt;= 3) {
          Callable&lt;MyResult&gt; callable = new Callable&lt;MyResult&gt;() {
              @Override
              public MyResult call() throws Exception {
                  return compute(tree, 0, exec);
              }
          };
          Future&lt;MyResult&gt; task = exec.submit(callable);
          return task.get();
      }
      else {
          MyResult result = new MyResult();
          if (something) {
              tree = tree.leftChild;
              result = compute(tree,depth+1,exec);
          }
          else if (something else){
              tree = tree.rightChild;
              result = compute(tree,depth+1,exec);
          }
          else
              return result;
      }
  }
  </code></pre>
  
  <p>What I expect to do is the recursive method will continue to calculate without waiting for the return value of task.get(). So, whenever it goes to depth 3, it submits a future task and then go back to calculate another child while the task will calculate its own subtree at the same time.</p>
  
  <p>However, I found that this method is still in sequence, not in parallel. (I printed out the depth every time the method was called, and the result was as same as the method without using future and executor, and it was always slower.)</p>
  
  <p>I believe this is not the correct way of using Future and Callable, I have found some examples, but they are not using a recursive method. Most common example is to have a list of Future List>, and submit a task each time, after that it iterates the Future list in another loop.</p>
  
  <p>Does anyone know how to implement Future and Executor in recursive method?</p>