Interrupting a thread

Interrupting a thread

In this tutorial, we are going to discuss about Interrupting a thread in java. In Java, interrupting a thread is a mechanism used to request the interruption of the thread’s execution. This mechanism is often used to gracefully stop a thread from running, allowing it to perform any necessary cleanup operations before exiting.

We can interrupt a sleeping or waiting thread by using interrupt method of thread class.

public void interrupt()
class MyThread extends Thread {
   public void run() {
      try {
         for (int i = 0; i < 5; i++) {
            System.out.println("Child Thread :" + i);
            Thread.sleep(2000);
         }
      } catch (InterruptedException e) {
         System.out.println("I got interrupted");
      }
   }
}

public class MyThreadInterrupt {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start();
      t.interrupt(); 
      System.out.println("Main thread");
   }
}

Output

Main thread
Child Thread :0
I got interrupted

Note

  • Whenever we are calling interrupt() method we may not see the effect immediately, if the target Thread is in sleeping or waiting state it will be interrupted immediately.
  • If the target Thread is not in sleeping or waiting state then interrupt call will wait until target Thread will enter into sleeping or waiting state. Once target Thread entered into sleeping or waiting state it will effect immediately.
  • In its lifetime if the target Thread never entered into sleeping or waiting state then there is no impact of interrupt call simply interrupt call will be wasted.

E.g

class MyThread extends Thread {
   public void run() {
      for (int i = 0; i < 5; i++) {
         System.out.println("Child Thread :" + i);
      }
      try {
         Thread.sleep(2000);
      } catch (InterruptedException e) {
         System.out.println("Thread got interrupted");
      }
   }
}

public class MyThreadInterrupt {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start();
      t.interrupt(); 
      System.out.println("Main thread");
   }
}

Output

Main thread
Child Thread :0
Child Thread :1
Child Thread :2
Child Thread :3
Child Thread :4
Thread got interrupted
  • In the above program interrupt() method call invoked by main Thread will wait until child Thread entered into sleeping state.
  • Once child Thread entered into sleeping state then it will be interrupted immediately.
Interrupting a thread

It’s important to note that interrupting a thread does not forcefully terminate it. It’s a cooperative mechanism where the interrupted thread should check its interruption status periodically and exit gracefully when interrupted. It’s up to the thread’s implementation to handle interruption properly and decide when to exit.

That’s all about how to Interrupting a thread in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

Interrupting a thread
Scroll to top