Interrupting a thread in Java Tutorial

In this section, we will learn what it means to interrupt a thread in Java and how to practically interrupt a thread.

What does it mean to interrupt a thread in Java?

Thread interruption is a process that threads can use to send a signal to each other.

For example, let’s say there are two threads and the first one is in charge of taking a network request and the other thread is in charge of running a task whenever the first thread got the request.

Here, the second thread needs to wait until the first thread asks for the second thread to run a task. So the way the first thread can signal the second thread is by using the interruption methods!

Note that there are multiple ways by which two threads can signal each other (For example by using a shared variable as a flag), and so, consider the interruption method as another method of implementing a communication between two threads.

Also, be aware that it’s up to a thread to how to react when it gets a signal from another thread. For example, it can simply ignore the signal and move on to do whatever it was doing.

Three methods related to thread interruption from Thread class:

In Java, there are three methods that can be used when it comes to interrupting a thread and checking if a thread was in fact interrupted.

These methods are:

  • interrupt()
  • interrupted()
  • isInterrupted()

Java Thread interrupt() Method

The interrupt() method is used to send a signal to a thread we want to interrupt.

When calling this method on a thread, a flag in that thread which is called `interrupted` will be set on. (The flag is a boolean value. If we call this method, the value of that flag become true).

Java Thread interrupt() syntax

This is the syntax of Thread interrupt() method in Java:

public void interrupt()

Java Thread interrupt() parameters

The method does not take an argument.

Java Thread interrupt() Return Value

The return value of this method is void.

Java Thread interrupt() Exception:

SecurityException: We get this exception if the current thread cannot modify the target thread (the one that we want to call the method on it)

Java Thread interrupted() Method

Using the Thread interrupted() method, a thread can check to see if another thread called the interrupt() method of this thread to basically send it an interruption signal.

Java Thread interrupted() syntax

public static boolean interrupted()

Java Thread interrupted() parameters

The method does not take an argument.

Java Thread interrupted() Return Value

The return value of this method is of type boolean.

If the thread that calls this method is in fact interrupted by another thread, then this method will return the value true. Otherwise, the value false will return instead.

Note that when calling this method, if the value true returned, calling the method for the second time will return the value false. This is because this method automatically changes the value of the interrupted flag to false after calling it (when the value of the interrupted flag was true).

Java Thread interrupted() Exception:

The method does not throw an exception.

Example: using Thread interrupted() method in Java

public class Main {

    public static void main(String[] args) {
        System.out.println("Is current thread interrupted?: "+Thread.currentThread().interrupted());
        Thread.currentThread().interrupt();
        System.out.println("Is current thread interrupted?: "+Thread.currentThread().interrupted());
        
        System.out.println("Is current thread interrupted?: "+Thread.currentThread().interrupted());

    }

}

Output:

Is current thread interrupted?: false

Is current thread interrupted?: true

Is current thread interrupted?: false

In the example above, the first time we call the `interrupted()` method, it returned the value true and this is because of the call to the `interrupt()` method over the thread. But we can see the second call to the interrupted() method returned the value false. Again, this is because this method changes the value of the interrupted flag to false after it gets the value true from it.

Java Thread isInterrupted() Method

The `isInterrupted()` method is a way of checking the value of the interrupted flag of a thread to see if the target thread is interrupted by another thread.

Now, the beauty of this method is the fact that it will not change the value of the `interrupted` flag after getting the value true from it. It will simply return either a value false or true depending whether the target thread was interrupted.

Java Thread isInterrupted() syntax

Here’s the syntax of the method:

public boolean isInterrupted()

Java Thread isInterrupted() parameters

The method does not take an argument.

Java Thread isInterrupted() Return Value

The return value of the method is of type boolean.

If the target thread was interrupted by another thread (hence its interrupted flag was set to true) then calling this method will return the value true, no-matter how many times we call the method. Otherwise, the value false will return as a result.

Java Thread isInterrupted() Exception:

The method does not throw an exception.

Example: using Thread isInterrupted() method in Java

public class Main {

    public static void main(String[] args) {
        System.out.println("Is current thread interrupted?: "+Thread.currentThread().interrupted());
        Thread.currentThread().interrupt();
        System.out.println("Is current thread interrupted? (checking with isInterrupted() method: "+Thread.currentThread().isInterrupted());
        System.out.println("Is current thread interrupted? (checking with isInterrupted() method: "+Thread.currentThread().isInterrupted());
        System.out.println("Is current thread interrupted? (checking with isInterrupted() method: "+Thread.currentThread().isInterrupted());
        System.out.println("Is current thread interrupted?: "+Thread.currentThread().interrupted());
        System.out.println("Is current thread interrupted?: "+Thread.currentThread().interrupted());

    }

}

Output:

Is current thread interrupted?: false

Is current thread interrupted? (checking with isInterrupted() method: true

Is current thread interrupted? (checking with isInterrupted() method: true

Is current thread interrupted? (checking with isInterrupted() method: true

Is current thread interrupted?: true

Is current thread interrupted?: false

As you can see, using the isInterrupted() method will not change the interrupted flag of the target thread, no-matter how many times we call this method. But the moment we call the `interrupted()` method, the interrupted status of the target thread will be cleared out and so the next time we check the status using either of these methods will get the value false.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies