参考:
https://artisan.blog.csdn.net/article/details/101174377
https://blog.csdn.net/jiadajing267/article/details/80137006
文章目录
一、使用停止标记(volatile变量)
public class StopThread_1 {
public static void main(String[] args) {
WorkThread workThread = new WorkThread();
workThread.start();
// main线程继续执行业务逻辑 假设运行了3秒
try {
System.out.println(Thread.currentThread().getName() + " 运行中");
Thread.sleep(3_000);
// 假设触发某个条件,需要退出WorkThread线程
workThread.shutdownThread();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class WorkThread extends Thread {
// 线程内部设置开关 volatile 多线程可见
private volatile boolean flag = true;
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " working , flag=" + flag);
// 通过接收flag来决定 终止或运行
while (flag) {
// do something ......
}
}
private void shutdownThread() {
this.flag = false;
System.out.println(Thread.currentThread().getName() + " set flag=" + flag);
}
}
}
运行结果:
二、发起中断、响应中断 (interrupt)
public class StopThread_2 {
public static void main(String[] args) {
WorkThread workThread = new WorkThread("workThread");
workThread.start();
try {
// 模拟主线程的业务
System.out.println(Thread.currentThread().getName() + " working...");
Thread.sleep(3_000);
// 假设触发了某种条件,需要中断workThread线程的执行 调用interrupt
workThread.interrupt();
System.out.println("workThread interrupt...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class WorkThread extends Thread {
public WorkThread(String name) {
super(name);
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " working ");
// 死循环
while (true) {
// 判断 该线程是否被打断
if (Thread.interrupted()) {
System.out.println(Thread.currentThread().getName() + " received interrupt signal...");
// break (break的话 还会执行 assume some logic is here的代码)
// return (return的话,如果有后面还有代码的话就不会执行后续的代码了)
break;
}
}
// assume some logic is here
System.out.println(Thread.currentThread().getName() + " working is still");
}
}
}
运行结果:
break
只是退出循环,并没有停止线程,有些人可能会想到用return
代替break
就行了,但是更常见的用法是抛出InterruptedException
,这样才能使得线程停止得以扩散:
public class StopThread_2 {
public static void main(String[] args) {
WorkThread workThread = new WorkThread("workThread");
workThread.start();
try {
// 模拟主线程的业务
System.out.println(Thread.currentThread().getName() + " working...");
Thread.sleep(3_000);
// 假设触发了某种条件,需要中断workThread线程的执行 调用interrupt
workThread.interrupt();
System.out.println("workThread interrupt...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class WorkThread extends Thread {
public WorkThread(String name) {
super(name);
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " working ");
// 死循环
while (true) {
// 判断 该线程是否被打断
if (Thread.interrupted()) {
System.out.println(Thread.currentThread().getName() + " received interrupt signal...");
// return;
throw new InterruptedException();
}
}
// assume some logic is here
// System.out.println(Thread.currentThread().getName() + " working is still"); 编译不通过 需注释
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行结果:
三、阻塞时中断线程会抛出InterruptException异常
1、先sleep,后interrupt
@Override
public void run() {
super.run();
try {
System.out.println( "begin run" );
Thread.sleep( 500 );
System.out.println( "begin end" );
} catch (InterruptedException e) {
System.out.println("在沉睡中终止");
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
MyThread5 thread5 = new MyThread5();
thread5.start();
Thread.sleep( 20 );
thread5.interrupt();
} catch (InterruptedException e) {
System.out.println( "main catch" );
e.printStackTrace();
}
}
从打印结果看,sleep状态下停止某一个线程,会进入catch语句,并清除状态值,变成false
2、先interrupt,后sleep
try {
for (int i = 0; i < 10000; i++) {
System.out.println( "i=" +(i + 1) );
}
System.out.println( "run begin" );
Thread.sleep( 200 );
System.out.println( "run end" );
} catch (InterruptedException e) {
System.out.println( "先停止,后sleep" );
e.printStackTrace();
}
public static void main(String[] args) {
MyThread5 thread5 = new MyThread5();
thread5.start();
thread5.interrupt();
}
从打印结果看,任务执行完成后,才抛出异常!
四、生产者、消费者要停止线程,得保证任务处理完
// 线程停止标记,保证可见性
protected volatile boolean inUse = true;
// 待处理任务计数器,保证并发下计数正确
public final AtomicInteger reservations = new AtomicInteger(0);
// 线程不再被需要,且无待处理任务 再停止任务
if (!inUse && reservations.get() <= 0) {// 语句③
break;
}