title: Java thread join in for loop, unknown behavior in output
body: <p>Here is the code creating an array of threads, starting them, and then calling join on them:</p>
  
  <pre><code>public class JoinExample {
  
  private static class CodeForThread implements Runnable {
      @Override
      public void run() {
          for (int i = 0; i &lt; 5; i++) {
              System.out.println(Thread.currentThread().getName() + " " + i);
          }
      }
  }
  
  public static void main(String[] args) throws InterruptedException {
      Thread[] threads = new Thread[100];
      for (int i = 0; i &lt; 100; i++){
          threads[i] = new Thread(new CodeForThread());
          threads[i].start();
      }
      for (int i = 0; i &lt; threads.length; i++) {
          threads[i].join();
      }
      System.out.println("All Done");
  }
  
  }
  </code></pre>
  
  <p>I know that join waits for a thread to die before moving on, and that the threads may be called out of order, but will still complete their task. The problem is I have so outliers in my output. Can someone please explain why something like this might happen: </p>
  
  <pre><code>Thread-60 0
  Thread-60 1
  Thread-60 2
  Thread-60 3
  Thread-60 4
  Thread-97 0 //***
  Thread-59 0
  Thread-59 1
  Thread-59 2
  Thread-59 3
  Thread-59 4
  </code></pre>
  
  <p>Is this because that was one of the threads that got scheduled to start before entering the join loop?</p>