Interrupting a Thread in Java: Methods and Best Practices
Learn how to manage thread interruption in Java to safely and effectively halt thread execution. Discover the methods interrupt(), interrupted(), and isInterrupted(), which provide control over interrupting threads at safe points during their execution.
Interrupting a Thread in Java
Thread interruption in Java is the process of stopping a thread's execution either immediately or at a safe point. Java provides methods to manage thread interruption, such as interrupt(), interrupted(), and isInterrupted().
Methods Provided by the Thread Class
The following methods are provided by the Thread class for interrupting a thread:
public void interrupt(): Interrupts the thread, causing it to break out of a sleeping or waiting state.public static boolean interrupted(): Returns the interrupted status of the current thread and clears the interrupted status flag.public boolean isInterrupted(): Returns the interrupted status of the thread without clearing the interrupted status flag.
Examples of Interrupting Threads
Example 1: Interrupting a Thread that Stops Working
In this example, after interrupting the thread, we propagate the InterruptedException, causing the thread to stop working.
FileName: TestInterruptingThread1.java
class TestInterruptingThread1 extends Thread {
public void run() {
try {
Thread.sleep(1000);
System.out.println("Task");
} catch(InterruptedException e) {
throw new RuntimeException("Thread interrupted..." + e);
}
}
public static void main(String args[]) {
TestInterruptingThread1 t1 = new TestInterruptingThread1();
t1.start();
try {
t1.interrupt();
} catch(Exception e) {
System.out.println("Exception handled " + e);
}
}
}
Output
Exception in thread "Thread-0"
java.lang.RuntimeException: Thread interrupted...java.lang.InterruptedException: sleep interrupted
at TestInterruptingThread1.run(TestInterruptingThread1.java:7)
Example 2: Interrupting a Thread that Continues Working
In this example, after interrupting the thread, we handle the InterruptedException gracefully, allowing the thread to continue its task.
FileName: TestInterruptingThread2.java
class TestInterruptingThread2 extends Thread {
public void run() {
try {
Thread.sleep(1000);
System.out.println("Task");
} catch(InterruptedException e) {
System.out.println("Exception handled " + e);
}
System.out.println("Thread is running...");
}
public static void main(String args[]) {
TestInterruptingThread2 t1 = new TestInterruptingThread2();
t1.start();
t1.interrupt();
}
}
Output
Exception handled java.lang.InterruptedException: sleep interrupted
Thread is running...
Example 3: Interrupting a Thread that Behaves Normally
If a thread is not in a sleeping or waiting state, calling interrupt() sets the interrupted flag. The behavior depends on how the thread handles this flag.
FileName: TestInterruptingThread3.java
class TestInterruptingThread3 extends Thread {
public void run() {
for(int i = 1; i <= 5; i++)
System.out.println(i);
}
public static void main(String args[]) {
TestInterruptingThread3 t1 = new TestInterruptingThread3();
t1.start();
t1.interrupt();
}
}
Output
1
2
3
4
5
Using isInterrupted() and interrupted() Methods
The isInterrupted() and interrupted() methods provide ways to check the interrupted status of a thread:
FileName: TestInterruptingThread4.java
public class TestInterruptingThread4 extends Thread {
public void run() {
for(int i = 1; i <= 2; i++) {
if(Thread.interrupted()) {
System.out.println("Code for interrupted thread");
} else {
System.out.println("Code for normal thread");
}
}
}
public static void main(String args[]) {
TestInterruptingThread4 t1 = new TestInterruptingThread4();
TestInterruptingThread4 t2 = new TestInterruptingThread4();
t1.start();
t1.interrupt();
t2.start();
}
}
Output
Code for interrupted thread
Code for normal thread
Code for normal thread
Code for normal thread