Interrupting a Thread:

Interrupting a Thread:

If any thread is in sleeping or waiting state (i.e. sleep() or wait() is invoked), calling the 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. Let’s first see the methods provided by the Thread class for thread interruption.

The 3 methods provided by the Thread class for interrupting a thread

  • public void interrupt()
  • public static boolean interrupted()
  • public boolean isInterrupted()

Example of interrupting a thread that stops working

In this example, after interrupting the thread, we are propagating it, so it will stop working. If we don’t want to stop the thread, we can handle it where sleep() or wait() method is invoked. Let’s first see the example where we are propagating the exception.
  1. class TestInterruptingThread1 extends Thread{
  2. public void run(){
  3. try{
  4. Thread.sleep(1000);
  5. System.out.println(“task”);
  6. }catch(InterruptedException e){
  7. throw new RuntimeException(“Thread interrupted…”+e);
  8. }
  9. }
  10. public static void main(String args[]){
  11. TestInterruptingThread1 t1=new TestInterruptingThread1();
  12. t1.start();
  13. try{
  14. t1.interrupt();
  15. }catch(Exception e){System.out.println(“Exception handled “+e);}
  16. }
  17. }

Test it Now

download this example
Output:Exception in thread-0  
       java.lang.RuntimeException: Thread interrupted...
       java.lang.InterruptedException: sleep interrupted
       at A.run(A.java:7)

Example of interrupting a thread that doesn’t stop working

In this example, after interrupting the thread, we handle the exception, so it will break out the sleeping but will not stop working.
  1. class TestInterruptingThread2 extends Thread{
  2. public void run(){
  3. try{
  4. Thread.sleep(1000);
  5. System.out.println(“task”);
  6. }catch(InterruptedException e){
  7. System.out.println(“Exception handled “+e);
  8. }
  9. System.out.println(“thread is running…”);
  10. }
  11. public static void main(String args[]){
  12. TestInterruptingThread2 t1=new TestInterruptingThread2();
  13. t1.start();
  14. t1.interrupt();
  15. }
  16. }

Test it Now

download this example
Output:Exception handled  
       java.lang.InterruptedException: sleep interrupted
       thread is running...

Example of interrupting thread that behaves normally

If thread is not in sleeping or 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.
  1. class TestInterruptingThread3 extends Thread{
  2. public void run(){
  3. for(int i=1;i<=5;i++)
  4. System.out.println(i);
  5. }
  6. public static void main(String args[]){
  7. TestInterruptingThread3 t1=new TestInterruptingThread3();
  8. t1.start();
  9. t1.interrupt();
  10. }
  11. }

Test it Now

Output:1
       2
       3
       4 
       5

What about isInterrupted and interrupted method?

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.
  1. public class TestInterruptingThread4 extends Thread{
  2. public void run(){
  3. for(int i=1;i<=2;i++){
  4. if(Thread.interrupted()){
  5. System.out.println(“code for interrupted thread”);
  6. }
  7. else{
  8. System.out.println(“code for normal thread”);
  9. }
  10. }//end of for loop
  11. }
  12. public static void main(String args[]){
  13. TestInterruptingThread4 t1=new TestInterruptingThread4();
  14. TestInterruptingThread4 t2=new TestInterruptingThread4();
  15. t1.start();
  16. t1.interrupt();
  17. t2.start();
  18. }
  19. }

Test it Now

Output:Code for interrupted thread
       code for normal thread
       code for normal thread
       code for normal thread
       
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Interrupting a Thread: If any thread is in sleeping or waiting state (i.e. sleep() or wait() is invoked), calling the 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 […]

Tags:

....
...