How to stop a thread in Java?
Stopping a thread in Java can be done in several ways, but it’s important to do it gracefully to ensure that the thread terminates safely without causing resource leaks or data corruption. The recommended approach is to use a flag to signal the thread to stop running, allowing it to complete its current iteration and then exit cleanly.
Java provides some deprecated methods like Thread.stop(), but using these methods is not recommended as they can leave the application in an inconsistent state.

Table of Contents
Key Points of Graceful Thread in Java
1. Use a Flag: Set a volatile boolean flag that the thread checks regularly to decide whether to continue running.
2. Interrupt the Thread: Use the interrupt() method to signal the thread to stop. The thread should handle InterruptedException properly.
3. Avoid Deprecated Methods: Avoid using Thread.stop(), Thread.suspend(), and Thread.resume() as they are unsafe and can cause issues.
Below is an example demonstrating how to stop a thread using a flag and interruption:
Using a Flag
java
class StoppableTask implements Runnable {
private volatile boolean running = true;
public void run() {
while (running) {
System.out.println("Thread is running...");
try {
Thread.sleep(1000); // Simulate work
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
break;
}
}
System.out.println("Thread stopped.");
}
public void stop() {
running = false;
}
public static void main(String[] args) {
StoppableTask task = new StoppableTask();
Thread thread = new Thread(task);
thread.start();
try {
Thread.sleep(5000); // Let the thread run for 5 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
task.stop(); // Signal the thread to stop
System.out.println("Stop signal sent.");
}
}
Using Interruption
java
class InterruptibleTask implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("Thread is running...");
try {
Thread.sleep(1000); // Simulate work
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
Thread.currentThread().interrupt(); // Preserve interrupt status
}
}
System.out.println("Thread stopped.");
}
public static void main(String[] args) {
Thread thread = new Thread(new InterruptibleTask());
thread.start();
try {
Thread.sleep(5000); // Let the thread run for 5 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.interrupt(); // Interrupt the thread
System.out.println("Interrupt signal sent.");
}
}
Explanation
Using a Flag
- StoppableTask implements Runnable and has a volatile boolean running flag.
- The run() method checks the running flag in a loop and performs work (simulated by Thread.sleep(1000)).
- The stop() method sets the running flag to false, signaling the thread to stop.
- In the main method, the thread runs for 5 seconds before the stop() method is called to stop the thread.
Using Interruption
- InterruptibleTask implements Runnable.
- The run() method checks the thread’s interrupt status using Thread.currentThread().isInterrupted() and performs work (simulated by Thread.sleep(1000)).
- If an InterruptedException is caught, the interrupt status is preserved by calling Thread.currentThread().interrupt().
- In the main method, the thread runs for 5 seconds before the interrupt() method is called to interrupt the thread.