I have doubt over Thread.interrupt() method and Thread.interrupted() method.
Please explain me the use of both these methods with some good example.
I am adding one code below taken from javabeat site
I want to know when will we reach "error" statement i.e exception line 1
public class Ques03 {
public static void main(
String[] args) {
InterruptThread
thread = new InterruptThread();
thread.start();
}
}
class InterruptThread extends Thread{
@Override
public void run() {
for (int i=0; i<=5 ; i++){
System.out.println("outside if"+i);
if (Thread.interrupted())
{
try
{
Thread.sleep(6000);
}catch(Exception e)
{
System.out.println("error");//line 1
}
System.out.println("Thread interrupted"+i);
}
if (i == 2 || i == 4)
{
System.out.println("inside if i==2 && i==4");
this.interrupt();
}
}
}
}
[ December 25, 2007: Message edited by: Maan Shenoy ]