Hi !
--->My doubt Is on relation ship between sleep method and interrupt status flag.
--->I am asking this doubt based on below two programmes.
--->In programme1, I have called sleep method with out clearing interrupt status .Then InterruptedException was thrown.
--->In programme2 ,I have called sleep method after clearing interrupt status flag using interrupted method.Then InterruptedException was not thrown.
Is there any relation ship between sleep method and interrupt status flag?
Does sleep method throws Interrupted exception based on interrupt status flag?
---------------------------------------------------------------
Programme1:
Output:class even extends Thread { public void run() { for(int i=10;i<=20;i=i+2) { System.out.println(i); this.interrupt(); try { Thread.sleep(1000); } catch(InterruptedException ie) { System.out.println("interrupted"); } } } } class interrupt1 { public static void main(String args[]) { even e1=new even(); e1.start(); } }
10
interrupted
12
interrupted
14
interrupted
16
interrupted
18
interrupted
20
Interrupted
---------------------------------------------------------------
Programme2:
output:class even extends Thread { public void run() { for(int i=10;i<=20;i=i+2) { System.out.println(i); this.interrupt(); Thread.interrupted(); try { Thread.sleep(1000); } catch(InterruptedException ie) { System.out.println("interrupted"); } } } } class interrupt2 { public static void main(String args[]) { even e1=new even(); e1.start(); } }
10
12
14
16
18
20
---------------------------------------------------------------