线程停止的三种方式超详细
线程停止的几种方式
1、简单粗暴法Stop
此方法已经废弃,不建议使用,jdk帮助文档有如下解释
1.1、从代码执行结果层面解释
package com.study.test;
public class ThreadStopTest {
public static void main(String[] args) throws InterruptedException {
StopTest stopTest = new StopTest();
stopTest.start();
Thread.sleep(100);
stopTest.stop();
}
}
class StopTest extends Thread{
@Override
public void run() {
try {
long start=System.currentTimeMillis();
//开始计数
for (int i = 0; i < 100000; i++){
System.out.println("runing.." + i);
Thread.sleep(10);
}
System.out.println((System.currentTimeMillis()-start)+"ms");
} catch (Throwable ex) {
System.out.println("Caught in run: " + ex);
ex.printStackTrace();
} finally {
System.out.println("run finally");
}
}
}
执行结果:抛出异常ThreadDeath(也说明了stop方法执行会暴力去结束线程,不管线程上的任务是否结束)
1.2、从抢占锁的角度考虑
package com.study.test;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) throws InterruptedException {
//定义锁对象
final Object lock = new Object();
// 先让线程t1持有锁,并且等待3秒然后释放
Thread t1 = new Thread(()->{
try {
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + " 拿到锁");
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName() + " 持有3s锁~逻辑处理中");
System.out.println(Thread.currentThread().getName() + " 释放锁");
}
} catch (Throwable ex) {
ex.printStackTrace();
}
});
// 线程2去排队等待锁
Thread t2 = new Thread(()->{
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + " 拿到锁");
}
});
// 拿锁持有3秒
t1.start();
Thread.sleep(100);
t1.stop();
t2.start();
}
}
执行结果:线程1本来应该持有锁三秒,但是因为调用线程1的stop方法马上释放掉锁
正常结果应该如下(注释掉t1.stop();逻辑代码)
1.3、总结
- 从执行代码结果的层面来看如果直接调用stop方法会即刻在线程的run()方法内抛出ThreadDeath异常,而且抛出的位置不确定,还有可能在catch或finally语句中。
- 并且直接使用stop停止线程是非常不明智的,如果突然释放锁会导致本来加锁的对象被其他线程抢到锁从而修改属性值,导致数据不一致的情况产生。
2、标志位法
这种方法的好处是让线程自己停下来,实现方式就是将一个变量作为标识,线程每隔一段时间去判断下这个变量是否停止为停止标识,如果是则停止,否则继续运行
2.1、代码实现
package com.study.test;
import java.sql.Time;
import java.util.concurrent.TimeUnit;
/**
* @author luosong
* @version 1.0
* @date 2021/2/24 12:44
*/
public class StopThread {
public static void main(String[] args) throws InterruptedException {
ThreadDemo threadDemo = new ThreadDemo();
Thread thread = new Thread(threadDemo);
thread.start();
TimeUnit.SECONDS.sleep(3);
threadDemo.stop();
}
}
class ThreadDemo implements Runnable{
public volatile boolean flag = true;
@Override
public void run() {
while (flag){
System.out.println("do... something");
}
}
public void stop(){
flag = false;
System.out.println("run... stop");
}
}
2.2、注意点
- volatile关键字,在多线程的情况下改变变量的值,如果需要马上通知其他线程,那么可以使用这个关键字,这是基于volatile关键字的特性—>可见性。
- 在正常开发中肯定是不会去使用死循环来去判断是否需要停止线程的,这个可以在关键业务逻辑中去判断
3、interrupt停止标记法
3.1、interrupt中断方法介绍
熟悉这种方法前必须先了解一组API
- public void interrupt() 中断线程
- public static boolean interrupted() 测试当前线程是否中断
- public boolean isInterrupted() 测试当前线程是否中断
从API文档中可以比较清晰的看出,判断线程是否已经被中断有两个方法,interrupted在判断线程是否中断的同时还清除了线程的中断状态
3.2、代码解析中断判断方法
3.2.1、interrupted
public class StopThread2 {
public static void main(String[] args) {
// 中断线程
Thread.currentThread().interrupt();
// 测试线程是否是中断状态,执行后将中断标志更改为false
// true
System.out.println(Thread.interrupted());
// false
System.out.println(Thread.interrupted());
// false
System.out.println(Thread.interrupted());
}
}
3.2.2、isInterrupted
public class StopThread2 {
public static void main(String[] args) {
// 中断线程
Thread.currentThread().interrupt();
// isInterrupted:测试线程是否是中断状态,执行后不更改状态标志
// true
System.out.println(Thread.currentThread().isInterrupted());
// true
System.out.println(Thread.currentThread().isInterrupted());
// true
System.out.println(Thread.currentThread().isInterrupted());
}
}
3.3、中断程序如何实现
其实interrupt中断方法和标志位法原理相似,但标识变量不需要自己定义
package com.study.test;
/**
* @author luosong
* @version 1.0
* @date 2021/2/24 22:40
*/
public class StopThread3 {
public static void main(String[] args) throws InterruptedException {
InterruptThread thread = new InterruptThread();
thread.start();
Thread.sleep(3000);
thread.stop();
}
}
class InterruptThread {
public Thread thread;
public void start(){
thread = new Thread(()->{
while (!thread.isInterrupted()){
System.out.println("do...something");
}
});
thread.start();
}
public void stop(){
thread.interrupt();
}
}
3.4、interrupt中断方法的特性
通过jdk文档可知,如果在线程阻塞时调用中断方法会抛出异常,同时清理中断状态
代码演示如下
/**
* @author luosong
* @version 1.0
* @date 2021/2/24 22:40
*/
public class StopThread2 {
public static void main(String[] args) throws InterruptedException {
InterruptThread1 taskCase3 = new InterruptThread1();
taskCase3.start();
Thread.sleep(3000);
taskCase3.stop();
}
}
class InterruptThread1 {
private Thread thread;
public void start() {
thread = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
try {
System.out.println("doSomething");
TimeUnit.MICROSECONDS.sleep(100);
} catch (InterruptedException e) {
// 重置中断标志位为true
// thread.interrupt();
e.printStackTrace();
System.out.println("抛出中断异常,中断程序");
}
}
});
thread.start();
}
public void stop() {
thread.interrupt();
}
}
注意:从jdk文档可知,如果interrupt中断的是阻塞的线程,那么就会导致抛出异常InterruptedException,并且会清理中断标志位,所以如上代码是一个死循环,如何解决呢?就是要重置标识位thread.interrupt();
,catch异常后要重置标识位即可解决问题
3.5、思考
现在可以了解到如果是线程阻塞然后中断就会抛出异常,那么假设线程先interrupt中断后再去阻塞会怎么样呢?
代码模拟
/**
* @author luosong
* @version 1.0
* @date 2021/2/24 22:40
*/
public class StopThread4 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
try {
// 数值尽可能的大
for (int i = 0; i < 10000; i++) {
System.out.println( "i=" +(i + 1) );
}
System.out.println( "开始阻塞" );
Thread.sleep( 200 );
System.out.println( "阻塞完毕" );
} catch (InterruptedException e) {
System.out.println( "中断报错·······" );
e.printStackTrace();
}
});
thread.start();
thread.interrupt();
}
}
结论:如果线程先中断然后进入阻塞同样会抛出异常
4、总结
本文总共介绍三种停止线程的方法,stop暴力方法肯定不推荐使用而且方法已经过时,通过对标志位法以及interrupt的对比明显可以得出,interrupt使用更加的健全,提供了API可以供我们使用。
posted on 2022-06-16 22:53 Java面试365 阅读(1738) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)