How to Properly Terminate Threads in Java: Using Flags, interrupt(), and the Deprecated stop() Method
This article explains three ways to terminate a running Java thread—using an exit flag with volatile, the deprecated stop() method, and interrupt()—and provides detailed code examples, usage guidelines, and the pitfalls of each approach.
In Java, terminating a thread before its task completes means abandoning the current operation. The article outlines three methods to stop a running thread: using an exit flag (often declared volatile), using the deprecated stop() method, and using interrupt() .
Using an exit flag – A simple example shows a Runnable implementation with a volatile boolean flag. The run() method checks this flag in a while loop and exits when the flag is set to false . The main method creates several threads, sleeps, then flips the flag to stop them.
public class MyRunnable implements Runnable {
//定义退出标志,true会一直执行,false会退出循环
//使用volatile目的是保证可见性,一处修改了标志,处处都要去主存读取新的值,而不是使用缓存
public volatile boolean flag = true;
public void run() {
System.out.println("第" + Thread.currentThread().getName() + "个线程创建");
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
//退出标志生效位置
while (flag) {
}
System.out.println("第" + Thread.currentThread().getName() + "个线程终止");
}
} public class TreadTest {
public static void main(String[] arg) throws InterruptedException {
MyRunnable runnable = new MyRunnable();
//创建4个线程
for (int i = 0; i < 4; i++) {
Thread thread = new Thread(myRunable, i + " ");
thread.start();
}
//线程休眠
Thread.sleep(2000L);
System.out.println("——————————————————————————");
//修改退出标志,使线程终止
runnable.flag = false;
}
}The output demonstrates that the threads stop after the flag is cleared.
Using interrupt() – The article clarifies that interrupt() only sets the interrupt status; it does not immediately halt execution. If a thread is blocked in sleep() or wait() , an InterruptedException is thrown, clearing the interrupt flag.
public class InterruptThread1 extends Thread {
public static void main(String[] args) {
try {
InterruptThread1 t = new InterruptThread1();
t.start();
Thread.sleep(200);
t.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
super.run();
for (int i = 0; i <= 200000; i++) {
System.out.println("i=" + i);
}
}
}Because the thread does not check its interrupt status, the loop continues. Adding a check with Thread.currentThread().isInterrupted() allows the thread to break out of the loop and terminate gracefully.
@Override
public void run() {
super.run();
for (int i = 0; i <= 200000; i++) {
if (Thread.currentThread().isInterrupted()) {
//处理中断逻辑
break;
}
System.out.println("i=" + i);
}
}The article also notes that Thread.sleep() throws InterruptedException , which clears the interrupt flag, so proper handling is required.
Using stop() – Although stop() can abruptly terminate a thread, it is unsafe and deprecated because it releases locks without cleanup, potentially causing data inconsistency. The article provides examples of why stop() should be avoided.
Finally, the article shows how a while‑true loop that blocks on a queue can be exited by setting a quit flag and calling interrupt() , demonstrating a practical pattern for graceful shutdown.
public void quit() {
mQuit = true; //更新标志位
interrupt(); //调用interrupt()方法中断阻塞并抛出一个异常
}Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.