How To Interrupt A Thread In Java
Interrupt a Thread
In Java, If thread is in sleeping state or waiting state then invoking the interrupt() method on the thread and breaks out the sleeping state or waiting state throwing InterruptedException and If the thread is not in the sleeping state or waiting state, invoking the interrupt() method executes normally and doesn't interrupt the thread but sets the interrupt flag to true.
In Java, If thread is in sleeping state or waiting state then invoking the interrupt() method on the thread and breaks out the sleeping state or waiting state throwing InterruptedException and If the thread is not in the sleeping state or waiting state, invoking the interrupt() method executes normally and doesn't interrupt the thread but sets the interrupt flag to true.
Methods
There are three methods provided by Thread class to interrupt a thread, which is given below.
public void interrupt()
public static boolean interrupted()
public boolean isInterrupted()
Let’s see an example, given below, of interrupting a thread which stops working.
Code
- public class InterruptThread extends Thread {
- public void run() {
- try {
- Thread.sleep(1500);
- System.out.println("work");
- } catch (InterruptedException e) {
- throw new RuntimeException("Thread interrupt" + e);
- }
- }
- public static void main(String args[]) {
- InterruptThread t1 = new InterruptThread();
- t1.start();
- try {
- t1.interrupt();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- }
Output
In the example, mentioned above, after interrupting the thread, we propagate it. Thus, it will stop working. If we don't want to stop the thread, we can handle it where sleep() method or wait() method is called.
Let’s see another example of interrupting a thread which doesn’t stop working.
Code
- public class InterruptThread extends Thread {
- public void run() {
- try {
- Thread.sleep(1500);
- System.out.println("work");
- } catch (InterruptedException e) {
- System.out.println("Exception :" + e);
- }
- System.out.println("Thread is running");
- }
- public static void main(String args[]) {
- InterruptThread t1 = new InterruptThread();
- t1.start();
- t1.interrupt();
- }
- }
Output
In the example, menti0oned above, after interrupting the thread, we handle the exception. Thus, it will break out the sleeping but will not stop working.
Let’s see an example of interrupting the thread, which performs normally.
Code
- public class InterruptThread extends Thread {
- public void run() {
- for (int i = 1; i <= 3; i++) {
- System.out.println(i);
- }
- }
- public static void main(String args[]) {
- InterruptThread t1 = new InterruptThread();
- t1.start();
- t1.interrupt();
- }
- }
Output
In the example, mentioned above, If the thread is not in sleeping state or waiting state, calling the interrupt() method sets the interrupted flag to true which can be used to stop the thread by the programmer later.
Difference between isInterrupted and interrupted method
The isInterrupted() method is used to return the interrupted flag either true or false.
The static interrupted() method is used to return the interrupted flag after which it sets the flag to false , if it is true.
Let’s see an example. Given below.
Code
- public class InterruptThread extends Thread {
- public void run() {
- for (int i = 1; i <= 2; i++) {
- if (Thread.interrupted()) {
- System.out.println("Interrupted thread");
- } else {
- System.out.println("Normal thread");
- }
- }
- }
- public static void main(String args[]) {
- InterruptThread t1 = new InterruptThread();
- InterruptThread t2 = new InterruptThread();
- t1.start();
- t1.interrupt();
- t2.start();
- }
- }
Output
Summary
Thus, we learnt that if the thread is in sleeping state or waiting state then invoking the interrupt() method on the thread and breaks out the sleeping state or waiting state throwing InterruptedException and If the thread is not in the sleeping state or waiting state, invoking the interrupt() method executes normally and also learnt how we can create it in Java.