title: how to execute join(); function on mainThread?
body: <p>I am trying to execute join(); function on main thread TO get a main thread object I have used a reference from Thread.currentThread(); by the following code but it often gives me <strong>a NullPointerException as if mainthread has not been intialized:</strong></p>
  
  <pre><code>    public class Main{
  
       MyThread t1 = new MyThread();
  
      public static void main(String[] args)  {
        t1.mainthread = Thread.currentThread();
  
      MyThread t = new MyThread();
      t.start();
  
      for (int i = 0; i&lt;10 ; i++) 
          System.out.println("main thread");
  
      }
  
      }
  </code></pre>
  
  <p>The child thread classs :</p>
  
  <pre><code>     public class MyThread extends Thread {
  
       Thread mainthread ;    
  
  @Override
  public void run() {
  
      for (int i = 0; i&lt;10 ; i++) 
          System.out.println("child thread ");
      try {
          mainthread.join();
      } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>It often gives me a NullPointerException as if mainthread has not been intialized</p>