Thread interruption in Java is a mechanism used to signal a thread to stop its execution or break out of a waiting or sleeping state.
In this chapter, we will learn how thread interruption works, the methods used for interruption, and different behaviors of interrupted threads with examples.
If a thread is in a sleeping or waiting state (using sleep() or wait()), calling the interrupt() method causes it to exit that state by throwing an InterruptedException.
If the thread is not in a sleeping or waiting state, calling interrupt() does not stop the thread but sets the interrupted flag to true, which can be checked later by the programmer.
Java provides built-in methods in the Thread class to interrupt a thread and check its interruption status.
The interrupt() method is used to interrupt a thread. If the thread is in a sleeping or waiting state, it throws an InterruptedException. Otherwise, it sets the interrupted flag to true.
Syntax:
Here is the syntax of the interrupt() method:
The interrupted() method checks whether the current thread has been interrupted and then clears the interrupted status if it is set.
Syntax:
Here is the syntax of the interrupted() method:
The isInterrupted() method checks whether a thread has been interrupted without changing the interrupted status.
Syntax:
Here is the syntax of the isInterrupted() method:
When a thread is in a sleeping or waiting state and the interrupt() method is called, an InterruptedException is thrown. If this exception is not handled properly and is propagated, the thread terminates immediately and stops its execution.
In this example, the interrupted thread propagates the exception that causes it to stop execution completely.
Output:
Exception in thread-0
java.lang.RuntimeException: Thread interrupted...
java.lang.InterruptedException: sleep interrupted
at A.run(A.java:7)
When a sleeping or waiting thread is interrupted, an InterruptedException is thrown. If the exception is caught and handled inside the thread, the thread exits the waiting state and continues executing the remaining code normally.
In this example, the interruption is handled properly, so the thread resumes execution after handling the exception.
Output:
Exception handled
java.lang.InterruptedException: sleep interrupted
thread is running...
If a thread is running normally and not in a sleeping or waiting state, calling interrupt() does not stop the thread. Instead, it sets the interrupted flag to true that allows the programmer to check the status and decide how the thread should respond.
The following example demonstrates that when a running thread is interrupted, it continues its execution normally, and only the interrupted flag is set to indicate the interruption.
Output:
1
2
3
4
5
The isInterrupted() method checks the interrupted status without changing it, whereas the static interrupted() method checks and clears the interrupted flag.
The following example demonstrates the use of the interrupted() method to check and clear the interrupted status of a thread during execution.
Output:
Code for interrupted thread
code for normal thread
code for normal thread
code for normal thread
We request you to subscribe our newsletter for upcoming updates.