title: Java Multithreading concept and join() method
body: <p>I'm confused in <code>join()</code> method used in Threads in Java. In the following code:</p>
  
  <pre><code>// Using join() to wait for threads to finish.
  class NewThread implements Runnable {
  
      String name; // name of thread
      Thread t;
  
      NewThread(String threadname) {
          name = threadname;
          t = new Thread(this, name);
          System.out.println("New thread: " + t);
          t.start(); // Start the thread
      }
  // This is the entry point for thread.
  
      public void run() {
          try {
              for (int i = 5; i &gt; 0; i--) {
                  System.out.println(name + ": " + i);
                  Thread.sleep(1000);
              }
          } catch (InterruptedException e) {
              System.out.println(name + " interrupted.");
          }
          System.out.println(name + " exiting.");
      }
  }
  
  class DemoJoin {
  
      public static void main(String args[]) {
          NewThread ob1 = new NewThread("One");
          NewThread ob2 = new NewThread("Two");
          NewThread ob3 = new NewThread("Three");
          System.out.println("Thread One is alive: "
                  + ob1.t.isAlive());
          System.out.println("Thread Two is alive: "
                  + ob2.t.isAlive());
          System.out.println("Thread Three is alive: "
                  + ob3.t.isAlive());
  // wait for threads to finish
          try {
              System.out.println("Waiting for threads to finish.");
              ob1.t.join();
              ob2.t.join();
              ob3.t.join();
          } catch (InterruptedException e) {
              System.out.println("Main thread Interrupted");
          }
          System.out.println("Thread One is alive: "
                  + ob1.t.isAlive());
          System.out.println("Thread Two is alive: "
                  + ob2.t.isAlive());
          System.out.println("Thread Three is alive: "
                  + ob3.t.isAlive());
          System.out.println("Main thread exiting.");
      }
  }
  </code></pre>
  
  <p>Sample output from this program is shown here:</p>
  
  <pre><code>New thread: Thread[One,5,main]
  New thread: Thread[Two,5,main]
  New thread: Thread[Three,5,main]
  Thread One is alive: true
  Thread Two is alive: true
  Thread Three is alive: true
  Waiting for threads to finish.
  One: 5
  Two: 5
  Three: 5
  One: 4
  Two: 4
  Three: 4
  One: 3
  Two: 3
  Three: 3
  One: 2
  Two: 2
  Three: 2
  One: 1
  Two: 1
  Three: 1
  Two exiting.
  Three exiting.
  One exiting.
  Thread One is alive: false
  Thread Two is alive: false
  Thread Three is alive: false
  Main thread Exiting
  </code></pre>
  
  <p>In the above code :</p>
  
  <ol>
  <li><p>I'm not able to understand the flow of execution of the program, And when <code>ob1</code> is created then the constructor is called where  <code>t.start()</code> is written but still <code>run()</code> method is not executed rather <code>main()</code> method continues execution. So why is this happening?</p></li>
  <li><p><code>join()</code> method is used to wait until the thread on which it is called does not terminates, but here in output we see alternate outputs of the thread why??</p></li>
  </ol>
  
  <p>And if the use of <code>join</code> is this then what is the use of <code>synchronized</code>??</p>
  
  <p>I know I'm missing a basic concept here, but I'm not able to figure it out so please help.</p>