title: Thread.join not working
body: <p>A call to join() Guaranteed to cause the current thread to stop executing until the thread it joins with
  (in other words, the thread it calls join() on) completes.</p>
  
  <p><strong>However, in my program both the threads are executing simultaneously. Thread1 is not waiting for Thread2 to finish its execution.</strong></p>
  
  <p><strong>What is wrong with my program?</strong></p>
  
  <pre><code>public class Launcher1 {
  
  
      public static void main(String[] args) {
          JoinExample runnable=new JoinExample();
  
          Thread thread1=new Thread(runnable);
          Thread thread2=new Thread(runnable);
  
          thread1.start();
  
          try {
              Thread.sleep(2000);
          } catch (InterruptedException e1) {
              e1.printStackTrace();
          }
  
          thread2.start();
          try {
              thread2.join();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
  
      }
  
  }
  
  public class JoinExample implements Runnable{
      public void run(){
          for(int i=0;i&lt;10;i++){
              System.out.println("i:"+i+" "+Thread.currentThread().getName());
  
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
  
  }
  </code></pre>
  
  <blockquote>
    <blockquote>
      <p>Output:</p>
      
      <p>i:0 Thread-0</p>
      
      <p>i:1 Thread-0</p>
      
      <p>i:0 Thread-1</p>
      
      <p>i:2 Thread-0</p>
      
      <p>i:1 Thread-1</p>
      
      <p>i:3 Thread-0</p>
      
      <p>i:4 Thread-0</p>
      
      <p>i:2 Thread-1</p>
      
      <p>i:3 Thread-1</p>
      
      <p>i:5 Thread-0</p>
      
      <p>i:4 Thread-1</p>
      
      <p>i:6 Thread-0</p>
      
      <p>i:7 Thread-0</p>
      
      <p>i:5 Thread-1</p>
      
      <p>i:8 Thread-0</p>
      
      <p>i:6 Thread-1</p>
      
      <p>i:7 Thread-1</p>
      
      <p>i:9 Thread-0</p>
      
      <p>i:8 Thread-1</p>
    </blockquote>
    
    <p>i:9 Thread-1</p>
  </blockquote>