Child thread call join method on main thread and main thread call join method on child thread so child thread has to wait until completion of main thread and main thread also has to wait until completion of child thread and this condition is known as deadlock.
class MyThread extends Thread {
static Thread t1;
public void run()
{
try
{
t1.join(); // call join method on thread t1
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println("child thread");
}
}
public class ThreadJoin
{
public static void main(String[] args) {
MyThread.t1=Thread.currentThread();
MyThread t2=new MyThread();
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("deadlock");
}
}
No comments:
Post a Comment