This exception is
thrown when a thread is in sleep mode and interrupted by another thread. Lets understand
this by an example:
create a java file Interrupt.java in dev21century.threading package:
package
dev21century.threading;
class
Thread1 extends Thread
{
Thread1(String
s)
{
super(s);
}
public void run()
{
System.out.println("Thread
Name: " + getName());
try{
Thread.sleep(60000
* 10);
}catch(Exception
e)
{}
System.out.println("interrupted
from class");
System.out.println("Thread
" + getName() + " dead..");
}
}
class
Thread2 extends Thread
{
Thread1
thread1;
Thread2(String
s, Thread1 thread1)
{
super(s);
this.thread1 =
thread1;
}
public void run()
{
System.out.println("Thread
Name : " +
getName());
thread1.interrupt();
try{
Thread.sleep(1000);
}catch(Exception
e) {}
System.out.println("Thread : " + getName() + "
dead.");
}
}
public class
Interrupt {
public static void
main(String args[])
{
Thread1
thread1 = new Thread1("thread1");
thread1.setPriority(10);
Thread2
thread2 = new Thread2("thread1",
thread1);
thread1.start();
thread2.start();
}
}
Output: