title: Can ForkJoinPool be made any faster than ExecutorService in this case?
body: <p>As long as I give ForkJoinPool <strong>one extra thread</strong> in the pool it performs equally faster as ExecutorService. Following are the three classes used: Main, RunnableTask and ForkJoinTask. Running on a 16 core box the program outputs following each time:
  <strong>Executor Time: 5002
  ForkJoin Time: 5002</strong></p>
  
  <p>Main Class:</p>
  
  <pre><code>import java.util.concurrent.CountDownLatch;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.concurrent.ForkJoinPool;
  
  public class Main {
  
      public static void main(String[] args) throws InterruptedException {
          runExecutor(80);
          runForkJoin(80);
      }
  
      public static void runForkJoin(int size) {
          ForkJoinPool fjp = new ForkJoinPool(17);
          long start = System.currentTimeMillis();
          fjp.invoke(new ForkJoinTask(size));
          System.out.println("ForkJoin Time: "
                  + (System.currentTimeMillis() - start));
          fjp.shutdown();
      }
  
      public static void runExecutor(int size) throws InterruptedException {
          ExecutorService exec = Executors.newFixedThreadPool(16);
          CountDownLatch latch = new CountDownLatch(size);
          long start = System.currentTimeMillis();
          for (int i = 0; i &lt; latch.getCount(); i++) {
              exec.submit(new RunnableTask(latch));
          }
          latch.await();
          System.out.println("Executor Time: "
                  + (System.currentTimeMillis() - start));
          exec.shutdown();
      }
  }
  </code></pre>
  
  <p>Runnable class:</p>
  
  <pre><code>import java.util.concurrent.CountDownLatch;
  
  public class RunnableTask implements Runnable {
      private CountDownLatch latch;
  
      public RunnableTask(CountDownLatch latch) {
          this.latch = latch;
      }
  
      @Override
      public void run() {
          try {
              Thread.sleep(1000);
              latch.countDown();
          } catch (Exception e) {
          }
      }
  }
  </code></pre>
  
  <p>RecursiveTask class:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  import java.util.concurrent.RecursiveTask;
  
  public class ForkJoinTask extends RecursiveTask {
      private List&lt;RecursiveTask&gt; tasks;
      private int size;
  
      public ForkJoinTask(int size) {
          super();
          this.tasks = new ArrayList&lt;&gt;();
          this.size = size;
      }
  
      @Override
      protected Object compute() {
          for (int i = 0; i &lt; size; i++) {
              RecursiveTask task = new RecursiveTask() {
                  @Override
                  protected Object compute() {
                      try {
                          Thread.sleep(1000);
                      } catch (Exception e) {
  
                      }
                      return null;
                  }
              };
              task.fork();
              tasks.add(task);
          }
  
          for (RecursiveTask task : tasks) {
              task.join();
          }
          return null;
      }
  }
  </code></pre>