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 a NullPointerException as if mainthread has not been intialized:
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<10 ; i++)
System.out.println("main thread");
}
}
The child thread classs :
public class MyThread extends Thread {
Thread mainthread ;
@Override
public void run() {
for (int i = 0; i<10 ; i++)
System.out.println("child thread ");
try {
mainthread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It often gives me a NullPointerException as if mainthread has not been intialized