Monday, 14 September 2015

Interrupting a Thread

Interrupting a Thread
If a thread sleeping or waiting (by invoking sleep() or wait() methods), calling interrupt() method on the thread, breaks out the sleeping or waiting state throwing InterruptedException.
If the thread is not in the sleeping or waiting state, calling the interrupt() method performs normal behaviour and doesn't interrupt the thread but sets the interrupt flag to true.

Methods

public void interrupt()

public static boolean interrupted()

Tests whether the current thread has been interrupted. If thread interrupted, the second call would return false.

A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.

Returns true if the current thread has been interrupted; false otherwise.

public boolean isInterrupted()

Tests whether this thread has been interrupted.
A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.

Returns true if this thread has been interrupted; false otherwise.

package com.threads.status;

public class TestInterruptingThread extends Thread{ 
       public void run(){ 
              try
                     Thread.sleep(1000); 
                     System.out.println("task"); 
              } catch(InterruptedException e) { 
                     throw new RuntimeException("Thread interrupted..."+e); 
              } 

       } 

       public static void main(String args[]){ 
              TestInterruptingThread thread = new TestInterruptingThread(); 
              thread.start(); 
              try
                     thread.interrupt(); 
              } catch(Exception e) {
                     System.out.println("Exception handled "+e);
              } 
       } 
}

Output:
Exception in thread "Thread-0" java.lang.RuntimeException: Thread interrupted...java.lang.InterruptedException: sleep interrupted at com.threads.status.TestInterruptingThread.run(TestInterruptingThread.java:9)



Interrupting a thread that doesn't stop working
If a thread is sleeping/waiting, calling a interrupt break out sleeping and handling the InterruptedException, it will not stop working.

package com.threads.status;

public class TestInterruptingThread2 extends Thread{ 
       public void run() { 
              try
                     Thread.sleep(1000); 
                     System.out.println("task"); 
              } catch(InterruptedException e)
                     System.out.println("Exception handled "+e); 
              } 
              System.out.println("thread is running...");
       }

       public static void main(String args[]) {
              TestInterruptingThread2 t1=new TestInterruptingThread2();
              t1.start();
              t1.interrupt();
       }
}
Output:
Exception handled java.lang.InterruptedException: sleep interrupted
thread is running..


Interrupting thread that behaves normally
If thread is not in sleeping/waiting state, calling the interrupt() method sets the interrupted flag to true that can be used to stop the thread by the java programmer later.

package com.threads.status;

class TestInterruptingThread3 extends Thread{ 

       public void run() { 
              for(int i=1;i<=5;i++) 
                     System.out.println(i); 
       } 

       public static void main(String args[]) { 
              TestInterruptingThread3 t1=new TestInterruptingThread3(); 
              t1.start(); 

              t1.interrupt(); 
       } 
}
Output:
1
2
3
4
5

isInterrupted() and interrupted() method example
The isInterrupted() method returns the interrupted flag either true or false.
The static interrupted() method returns the interrupted flag afterthat it sets the flag to false if it is true.

package com.threads.status;

public class TestInterruptingMethod extends Thread {

public void run() { 
    for(int i=1;i<=2;i++){ 
       if(Thread.interrupted()) { 
          System.out.println("interrupted :"+ Thread.currentThread().getName()); 
       } else {
          System.out.println("un-interrupted :"+ Thread.currentThread().getName());
       }
     }

       public static void main(String args[]){ 

              TestInterruptingMethod t1=new TestInterruptingMethod();
              TestInterruptingMethod t2=new TestInterruptingMethod();
             
              t1.setName("first");
              t1.start(); 
              t1.interrupt();
              if(t1.isInterrupted()) {
                     System.out.println("thread t1 is interrupted");
              }
             
              t2.setName("second");
              t2.start();
              if(t2.isInterrupted()) {
                     System.out.println("thread t1 is interrupted");
              }
       }
}
Output:
thread t1 is interrupted
interrupted :first
un-interrupted :first
un-interrupted :second
un-interrupted :second

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...