title: Why is my concurrent program slower than the sequential version?
body: <p><em>I was trying to do some analysis on calling <code>start</code> and <code>join</code> simultaneously,</em></p>
  
  <pre><code>    //Starting and Joining 
      for (Thread thread : threadArray) {
          thread.start();
          thread.join();
      }
  </code></pre>
  
  <p><em>compared with <code>start</code> first and then <code>join</code>.</em></p>
  
  <pre><code>    //Starting Them
      for (Thread thread : threadArray) {
          thread.start();
      }
      //Joining Them
      for (Thread thread : threadArray) {
          thread.join();
      }
  </code></pre>
  
  <p><em>What would be the performance difference between the above two cases?.</em></p>
  
  <p>In the first scenario, I am pretty much guaranteeing that the order of execution is sequential between threads. So if I have <code>n</code> threads and say each thread takes <code>Ti</code> time to complete the task, my total time of execution should be sum of <code>Tis</code> from 1 to n.</p>
  
  <p>In the second scenario, I am starting off and then joining. This is the part which I am getting confused. Shouldn't the time be almost same as above?.  What I am seeing is almost double on my machine.</p>
  
  <p>The entire code sample I am using is given below. </p>
  
  <pre><code>public class ThreadJoin implements Runnable {
  
      public void run() {
          for (int i=0;i&lt;10000000;i++) {
              //Random mathematical stuff independent of i.
               int ran = (int) (Math.random()*1000 -34)%47;
          }
      }
  
      public static void main(String[] args) throws Exception {
          Thread[] threadArray = new Thread[10];
          //Creating threads and feeding them with the job
          for (int i=0;i&lt;10;i++) {
              threadArray[i] = new Thread(new ThreadJoin());
          }
          long currentTimeMillis = System.currentTimeMillis();
          System.out.println("Started at " + currentTimeMillis);
          //Starting Them
          for (Thread thread : threadArray) {
              thread.start();
          }
          //Joining Them
          for (Thread thread : threadArray) {
              thread.join();
          }
          long currentTimeMillis2 = System.currentTimeMillis();
          System.out.println("Ended at " + currentTimeMillis2);
          System.out.println("Diff : " +( currentTimeMillis2 - currentTimeMillis));
      }
  
  }
  </code></pre>