Wednesday, August 5, 2015

Pending Interuppts in Java

     Interupts can be kept pending till the first Sleep call . A thread can continue to execute even if it is interrupted and throws an InterupttedException on the call of sleep method :


 Have a look at a sample code:

   Public class TestInteruppt{
     public static void main(String args[]){
    Thread.currentThread.interrupt();
try{
   Thread.sleep(30);
}catch(InterruptedException E){
 System.out.println(" This is a Pending Interupt");
 }

 }
 }

Difference between Thread.interrupted() and isInterrupted


       Thread.interrupted() is a final static method which is defined in the base thread class  and isInteruptted is a non static method in the thread class. Both returns the interrupt status of the thread.  One is called on the object of the thread to get the status since the interrupted() method is static this is called by Thread.interrupted().
      Thread.interrupted() returns true if the thread has interrupt flag set and resets the interrupt flag. while isInterrupted() method is a check for the flag and doesnt resets the flag