title: How does join() work in java? Does it guarantee the execution before main()?
body: <p>I am trying to understand the code flow with join().</p>
  
  <pre><code>public class Multi extends Thread {
  
      public void run() {
          for (int i = 0; i &lt; 5; i++) {
              System.out.println(Thread.currentThread().getName());
          }
      }
  
      public static void main(String[] args) {
  
          Thread t1 = new Multi();
          Thread t2 = new Multi();
          Thread t3 = new Multi();
          Thread t4 = new Multi();
  
          t1.start();
          try {
              t1.join();
          } catch (Exception e) {
          }
  
          t2.start();        
          t3.start();
          try {
              t3.join();
          } catch (Exception e) {
          }        
          t4.start();
  
          System.out.println("........" + Thread.currentThread().getName());
  
          t1.setName("A");
          t2.setName("B");
          t3.setName("C");
          t4.setName("D");
      }
  }
  </code></pre>
  
  <p>The output is always, as i observe after running program many times, that <code>thread t1</code> executes first and it will <code>complete it's execution</code> without any context switching, and whenever <code>t3 will start it will completes it's execution</code>. Is my understanding clear?</p>
  
  <p>I observe something that, if no join is used, <code>main()</code> executes anywhere b/w the execution of threads, means i see <code>........main</code> output in between the outputs of my program, but after <code>join()</code> it executes always after thread t3. Here is my doubt as <code>main()</code> starts before the <code>join()</code> syntax, so it should not follow the t3/t1 thread completion? Does it make sense or something I am missing?</p>