title: Java RecursiveTask fork vs compute
body: <p>i have code of febunacci algorithm using RecursiveTask i found it in</p>
  
  <p><a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/RecursiveTask.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/RecursiveTask.html</a></p>
  
  <p>Code 1</p>
  
  <pre><code>public class Fibonacci extends RecursiveTask&lt;Integer&gt; {
     final int n;
     public Fibonacci (int n) { this.n = n; }
     public Integer compute() {
       if (n &lt;= 1)
          return n;
       Fibonacci  f1 = new Fibonacci(n - 1);
       f1.fork();
       Fibonacci  f2 = new Fibonacci(n - 2);
   return f2.compute() + f1.join();
  
     }
     public static void main(String args[]){
         ForkJoinPool fjpool = new ForkJoinPool();
  
          RecursiveTask task = new Fibonacci(30);
          long startTime1=System.currentTimeMillis();;
          Integer O=(Integer) fjpool.invoke(task);
          long endTime1 =  System.currentTimeMillis();
          long duration1 = (endTime1 - startTime1);
          System.out.println(duration1);
  
     }}
  </code></pre>
  
  <p>this code give executed in 83ms i modified it </p>
  
  <p>Code 2</p>
  
  <pre><code>public class Fibonacci extends RecursiveTask&lt;Integer&gt; {
     final int n;
     public Fibonacci (int n) { this.n = n; }
     public Integer compute() {
       if (n &lt;= 1)
          return n;
       Fibonacci  f1 = new Fibonacci(n - 1);
  
       Fibonacci  f2 = new Fibonacci(n - 2);
   return f2.compute() + f1.compute();
  
     }
     public static void main(String args[]){
         ForkJoinPool fjpool = new ForkJoinPool();
  
          RecursiveTask task = new Fibonacci(30);
          long startTime1=System.currentTimeMillis();;
          Integer O=(Integer) fjpool.invoke(task);
          long endTime1 =  System.currentTimeMillis();
          long duration1 = (endTime1 - startTime1);
          System.out.println(duration1);
  
     }}
  </code></pre>
  
  <p>Now this code executed in 20ms can someone explain me why the second is faster i read the documentation it said that fork excutes asynchronously then why it is running slower then using compute.</p>