title: Java does not use all available CPUs
body: <p>I have a long running calculation which I need to carry out for a long list of inputs. The calculations are independent, so I would like to distribute them to several CPUs. I am using Java 8.</p>
  
  <p>The skeleton of the code looks like this:</p>
  
  <pre><code>ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
  
  MyService myService = new MyService(executorService);
  
  List&lt;MyResult&gt; results =
              myInputList.stream()
                       .map(myService::getResultFuture)
                       .map(CompletableFuture::join)
                       .collect(Collectors.toList());
  
  executorService.shutdown();
  </code></pre>
  
  <p>The main function responsible the calculation looks like this:</p>
  
  <pre><code>CompletableFuture&lt;MyResult&gt; getResultFuture(MyInput input) {
      return CompletableFuture.supplyAsync(() -&gt; longCalc(input), executor)))
  }
  </code></pre>
  
  <p>The long running calculation is stateless and does not do any IO.</p>
  
  <p>I would expect this code to use all available CPUs, but it does not happen. For example, on a machine with 72 CPUs and <code>numThreads=72</code> (or even eg. <code>numThreads=500</code>), cpu usage is at most 500-1000%, as shown by htop:</p>
  
  <p><a href="https://i.stack.imgur.com/PjpLT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/PjpLT.png" alt="htop"></a></p>
  
  <p>According to the thread dump, many of the calculation threads are waiting, ie.:</p>
  
  <pre><code>"pool-1-thread-34" #55 prio=5 os_prio=0 tid=0x00007fe858597890 nid=0xd66 waiting on condition [0x00007fe7f9cdd000]
     java.lang.Thread.State: WAITING (parking)
      at sun.misc.Unsafe.park(Native Method)
      - parking to wait for  &lt;0x0000000381815f20&gt; (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
      at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
      at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
      at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
      at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  
     Locked ownable synchronizers:
      - None
  </code></pre>
  
  <p>All calculation threads were waiting for the same lock. At the time of the dump, only 5 calculation threads were RUNNABLE, the rest were WAITING.</p>
  
  <p>What can be the reason for the locks and why do I not manage to use all cpus?</p>