title: Why is the total runtime slower when more threads were created as compared to lesser number of threads
body: <p>I am playing around multithreading to understand it properly. So I was trying to test how "number of threads being created" can impact the total runtime of a given task.</p>
  
  <pre><code>class Worker implements Runnable {
  
      private int id;
  
      public Worker(int id) {
          this.id = id;
      }
  
      @Override
      public void run() {
          System.out.println("Starting thread -&gt;" + Thread.currentThread().getName() + " with ID: " + id);
          System.out.println("Finishing thread -&gt;" + Thread.currentThread().getName() + " with ID: " + id);
      }
  }
  
  public class App {
  
      public static void main(String[] args) throws InterruptedException {
          App app = new App();
          ExecutorService threadPool;
          Map&lt;Integer, Long&gt; performanceMetric = new HashMap&lt;&gt;();
          for (int j = 3; j &gt; 0; j -= 1) {
              long begin = System.currentTimeMillis();
  </code></pre>
  
  <p>So here I am creating highest number of threads by assigning a high value on the loop. Am simply using the value of "j" from the loop and assigning it to the Executor.</p>
  
  <pre><code>            threadPool = Executors.newFixedThreadPool(j);
              System.out.println("\nTotal threads created: " + j);
              app.performService(threadPool);
              threadPool.shutdown();
  </code></pre>
  
  <p>Ensuring that the executor (threadpool) is shutdown first before calculating the total time taken.</p>
  
  <pre><code>            threadPool.awaitTermination(1, TimeUnit.SECONDS);
  </code></pre>
  
  <p>Here am calculating the total time taken for completing the task. The total time wont be calculated unless all the tasks are complete.</p>
  
  <pre><code>            long totalTime = (System.currentTimeMillis() - begin);
              System.out.println("Is the ThreadPool shutdown: " + (threadPool.isTerminated() ? "Yes" : "No"));
              performanceMetric.put(j, totalTime);
              System.out.println("\nTotal time elapsed when using " + j + " threads is " + totalTime + " milliseconds");
          }
          Set&lt;Integer&gt; keySet = performanceMetric.keySet();
          for (int key : keySet) {
              System.out.println("\nSUMMARY\n\nTotal time elapsed when using " + key + " threads is "
                      + performanceMetric.get(key) + " milliseconds");
          }
      }
  
      public void performService(ExecutorService threadPool) {
          for (int i = 0; i &lt; 5; i++) {
              threadPool.submit(new Worker(i));
          }
      }
  }
  </code></pre>
  
  <p>The output for the above program is below</p>
  
  <p>Begin output</p>
  
  <pre><code>Total threads created: 3
  Starting thread ---pool-1-thread-1 with ID: 0
  Starting thread ---pool-1-thread-3 with ID: 2
  Starting thread ---pool-1-thread-2 with ID: 1
  Finishing thread ---pool-1-thread-3 with ID: 2
  Finishing thread ---pool-1-thread-1 with ID: 0
  Finishing thread ---pool-1-thread-2 with ID: 1
  Starting thread ---pool-1-thread-3 with ID: 3
  Starting thread ---pool-1-thread-2 with ID: 4
  Finishing thread ---pool-1-thread-3 with ID: 3
  Finishing thread ---pool-1-thread-2 with ID: 4
  Is the ThreadPool shutdown: Yes
  
  Total time elapsed when using 3 threads is 4 milliseconds
  
  Total threads created: 2
  Starting thread ---pool-2-thread-1 with ID: 0
  Finishing thread ---pool-2-thread-1 with ID: 0
  Starting thread ---pool-2-thread-1 with ID: 2
  Finishing thread ---pool-2-thread-1 with ID: 2
  Starting thread ---pool-2-thread-2 with ID: 1
  Finishing thread ---pool-2-thread-2 with ID: 1
  Starting thread ---pool-2-thread-1 with ID: 3
  Finishing thread ---pool-2-thread-1 with ID: 3
  Starting thread ---pool-2-thread-2 with ID: 4
  Finishing thread ---pool-2-thread-2 with ID: 4
  Is the ThreadPool shutdown: Yes
  
  Total time elapsed when using 2 threads is 0 milliseconds
  
  Total threads created: 1
  Starting thread ---pool-3-thread-1 with ID: 0
  Finishing thread ---pool-3-thread-1 with ID: 0
  Starting thread ---pool-3-thread-1 with ID: 1
  Finishing thread ---pool-3-thread-1 with ID: 1
  Starting thread ---pool-3-thread-1 with ID: 2
  Finishing thread ---pool-3-thread-1 with ID: 2
  Starting thread ---pool-3-thread-1 with ID: 3
  Finishing thread ---pool-3-thread-1 with ID: 3
  Starting thread ---pool-3-thread-1 with ID: 4
  Finishing thread ---pool-3-thread-1 with ID: 4
  Is the ThreadPool shutdown: Yes
  
  Total time elapsed when using 1 threads is 0 milliseconds
  
  Total time elapsed when using 1 threads is 0 milliseconds
  
  Total time elapsed when using 2 threads is 0 milliseconds
  
  Total time elapsed when using 3 threads is 4 milliseconds
  </code></pre>
  
  <p>end output</p>
  
  <p>I am surprised how one or two threads performed faster compared to three threads. Can someone please explain.</p>