INTERRUPTING THREADS IN JAVA
In Java Threads, 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 behavior and doesn't interrupt the thread but sets the interrupt flag to
true.
interrupt() method: If any thread is in sleeping or waiting for a state then using the interrupt()
method, we can interrupt the execution of that thread by showing InterruptedException. A thread
that is in the sleeping or waiting state can be interrupted with the help of the interrupt() method of
Thread class.
Example: Suppose there are two threads and If one of the threads is blocked in an invocation of the
wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long,
int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and
it will receive an InterruptedException, which gives the chance to another thread to execute the
corresponding run() method of another thread which results into high performance and reduces the
waiting time of the threads.
Different scenarios where we can interrupt a thread
Case 1: Interrupting a thread that doesn't stop working: In the program, we handle the
InterruptedException using try and catch block, so whenever any thread interrupts the currently
executing thread it will come out from the sleeping state but it will not stop working.
// Java Program to illustrate the
// concept of interrupt() method
// while a thread does not stops working
class MyClass extends Thread {
public void run()
try {
for (int i = 0; i < 5; i++) {
[Link]("Child Thread executing");
// Here current threads goes to sleeping state
// Another thread gets the chance to execute
[Link](1000);
catch (InterruptedException e) {
[Link]("InterruptedException occur");
}
}
class Test {
public static void main(String[] args)
throws InterruptedException
MyClass thread = new MyClass();
[Link]();
// main thread calls interrupt() method on
// child thread
[Link]();
[Link]("Main thread execution completes");
Output
Main thread execution completes
Child Thread executing
InterruptedException occur
Case 2: Interrupting a thread that stops working: In the program, after interrupting the currently
executing thread, we are throwing a new exception in the catch block so it will stop working.
// Java Program to illustrate the
// concept of interrupt() method
// while a thread stops working
class Geeks extends Thread {
public void run()
try {
[Link](2000);
[Link]("Geeksforgeeks");
}
catch (InterruptedException e) {
throw new RuntimeException("Thread " +
"interrupted");
public static void main(String args[])
Geeks t1 = new Geeks();
[Link]();
try {
[Link]();
catch (Exception e) {
[Link]("Exception handled");
Output
Exception in thread "Thread-0" [Link]: Thread interrupted
at [Link]([Link])
Case 3: Interrupting a thread that works normally: In the program, there is no exception occurred
during the execution of the thread. Here, interrupt only sets the interrupted flag to true, which can
be used by Java programmers later.
// Java Program to illustrate the concept of
// interrupt() method
class Geeks extends Thread {
public void run()
for (int i = 0; i < 5; i++)
[Link](i);
public static void main(String args[])
Geeks t1 = new Geeks();
[Link]();
[Link]();