I am not sure how the method interrupt()works...
public class InterruptThread extends Object implements Runnable
{
public void run()
{
try
{ System.out.println("New
Thread is about to sleep for 20 seconds.");
Thread.sleep(20000);
} catch(InterruptedException e)
{
System.out.println("in run()- interrupted while sleeping");
return;
}
System.out.println("in run() - doing stuff after nap");
}
public static void main(
String args[])
{
InterruptThread it=new InterruptThread();
Thread t=new Thread(it, "child thread");
t.start();
try
{Thread.sleep(20000);
}
catch(InterruptedException e) {System.out.println("in run()- interrupted while sleeping");}
System.out.println("in main()-about to interrupt child thread");
t.interrupt();
System.out.println("in main() - leaving the main thread");
}
}
I have 2 questions here. 1. Is it necessary to extend class Object in this case or is it implicitly done.
2. In the main method of the class, does the t.interrupt act as a main thread and not as a child thread in this case. In this case, t,interrupt() interrupts the child thread.