Java 如何正确停止一个线程
自己在做实验性小项目的时候,发现自己遇到一个问题:如何控制线程的"死亡"?
首先,如何开启一个线程呢?
最简单的代码:
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("当前线程:" + Thread.currentThread() + ",当前时间戳:" + System.currentTimeMillis());
}
});
thread.start();
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程结束");
}
}
很简单,调用.start()方法,这个线程就会启动.
那么怎样主动去停止一个线程呢?要解答这个问题,首先要考虑:为什么要结束一个线程.
理由如下:
- 线程是JVM宝贵的资源,有的线程会长时间占用资源.
- 一些业务逻辑下会出现一个线程从逻辑上完全没有意义(比如一个定时器,调用了结束的方法),确实需要去停止.
结束一个线程有一个最基本的方法:Thread.stop()方法:
@Deprecated
public final void stop() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
checkAccess();
if (this != Thread.currentThread()) {
security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
}
}
// A zero status value corresponds to "NEW", it can't change to
// not-NEW because we hold the lock.
if (threadStatus != 0) {
resume(); // Wake up thread if it was suspended; no-op otherwise
} // The VM can handle all thread states
stop0(new ThreadDeath());
}
但是这个方法已经是:@Deprecated的了,也就是被建议不要使用的方法.
为什么不建议使用这个方法的官方说明: http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
实际上,我结合自己经验提出以下几点:
- 线程会直接停掉,按照代码逻辑要释放的资源,要调用的接口可能不会被运行(finally块的代码还是会执行)
- 会破坏锁,导致线程不安全(停掉一个线程就会释放它持有的锁,但不能保证逻辑上)
这两点都是非常严重的问题了.即使再小心,去调用stop()也会导致各种各样的问题.
如果不能"硬"实现结束线程,那么就可以考虑下"软"实现.
首先,导致一个线程长时间运行的原因无非有这么几个:
- 代码逻辑混乱\业务复杂,写了一个很长的run()方法
- 长时间睡眠
- 循环
对于这第一种嘛,只能从代码结构上优化了.
而这第二种,就要使用Thread.interrupt()方法了.这个方法唤醒睡眠中的线程:
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
TimeUnit.DAYS.sleep(1L);
System.out.println("睡眠结束");
} catch (Exception e) {
System.out.println("异常:" + e);
} finally {
System.out.println("finally块被执行");
}
}
});
thread.start();
if (!thread.isInterrupted()) {
thread.interrupt();
}
System.out.println("主线程结束");
}
}
在bio的服务端里面,会阻塞当前线程.监听的端口有消息,才会继续执行,而如果没有人连接,就需要通过这种方式来打断正在监听的线程.
对于这第三种,需要通过轮询标志位来控制退出.自己写的定时器代码:
public class TimerImpl implements Timer {
private static final Logger logger = LoggerFactory.getLogger(TimerImpl.class);
// 定时器线程
private TimerThread timerThread = null;
private volatile Boolean running = false;
private Handler taskHandler;
private Long time;
private TimeUnit unit;
@Override
public void start() throws Exception {
// 给参数生成本地固定拷贝,以确保在运行过程中,不会给参数的改变干扰
Handler curTask = taskHandler.getClass().newInstance();
Long curTime = new Long(time);
TimeUnit curUnit = unit.getClass().newInstance();
// 检查
if (ParameterUtil.checkNull(curTask, curTime, curUnit)) {
throw new Exception("定时器参数配置错误");
}
if (!running) {
synchronized (running) {
if (!running) {
timerThread = new TimerThread();
timerThread.setTaskHandler(curTask);
timerThread.setTime(curTime);
timerThread.setUnit(curUnit);
timerThread.start();
running = true;
}
}
}
}
@Override
public void stop() throws Exception {
if (!running) {
throw new Exception("定时器尚未开始");
}
synchronized (running) {
if (running) {
// 标志位
timerThread.cancel();
// 打断睡眠
if (!timerThread.isInterrupted()) {
timerThread.interrupt();
}
running = false;
}
}
}
private class TimerThread extends Thread {
private volatile boolean stop = false;
private Handler taskHandler;
private Long time;
private TimeUnit unit;
@Override
public void run() {
// circle
while (!stop) {
// sleep
try {
unit.sleep(time);
} catch (InterruptedException e) {
logger.info("定时线程被打断,退出定时任务");
stop = true;
return;
}
// do
try {
taskHandler.execute();
} catch (Exception e) {
logger.error("handler执行异常:{}", this.getClass(), e);
}
}
}
public void cancel() {
stop = true;
}
public Handler getTaskHandler() {
return taskHandler;
}
public void setTaskHandler(Handler taskHandler) {
this.taskHandler = taskHandler;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
public TimeUnit getUnit() {
return unit;
}
public void setUnit(TimeUnit unit) {
this.unit = unit;
}
}
@Override
public void setTimer(Long time, TimeUnit unit) {
this.time = time;
this.unit = unit;
}
@Override
public void setHandler(Handler handler) {
this.taskHandler = handler;
}
}
想要停止一个线程的方法是有的,但是会麻烦一些.
Java 如何正确停止一个线程的更多相关文章
- java如何正确停止一个线程
Thread类中有start(), stop()方法,不过stop方法已经被废弃掉. 平时其实也有用过,共享一个变量,相当于标志,不断检查标志,判断是否退出线程 如果有阻塞,需要使用Thread的in ...
- Java线程状态、线程start方法源码、多线程、Java线程池、如何停止一个线程
下面将依次介绍: 1. 线程状态.Java线程状态和线程池状态 2. start方法源码 3. 什么是线程池? 4. 线程池的工作原理和使用线程池的好处 5. ThreadPoolExecutor中的 ...
- Java并发(基础知识)—— 创建、运行以及停止一个线程
在计算机世界,当人们谈到并发时,它的意思是一系列的任务在计算机中同时执行.如果计算机有多个处理器或者多核处理器,那么这个同时性是真实发生的:如果计算机只有一个核心处理器那么就只是表面现象. 现代所有的 ...
- Java再学习——停止一个正在运行的线程
关于这个问题,先了解一下Thread类方法中被废弃的那些方法.suspend(), resume(),stop()/stop(Throwable obj),destroy() 首先,stop(Thro ...
- java停止一个线程
Thread类中有start(), stop()方法,不过stop方法已经被废弃掉. 平时其实也有用过,共享一个变量,相当于标志,不断检查标志,判断是否退出线程 如果有阻塞,需要使用Thread的in ...
- 别指望一文读懂Java并发之从一个线程开始
Understanding concurrent programming is on the same order of difficulty as understanding object-orie ...
- java holdsLock()方法检测一个线程是否拥有锁
http://blog.csdn.net/w410589502/article/details/54949506 java.lang.Thread中有一个方法叫holdsLock(),它返回true如 ...
- JAVA多线程之当一个线程在执行死循环时会影响另外一个线程吗?
一,问题描述 假设有两个线程在并发运行,一个线程执行的代码中含有一个死循环如:while(true)....当该线程在执行while(true)中代码时,另一个线程会有机会执行吗? 二,示例代码(代码 ...
- Java自学-多线程 启动一个线程
Java 创建一个线程的三种方式 多线程即在同一时间,可以做多件事情. 创建多线程有3种方式,分别是继承线程类,实现Runnable接口,匿名类 步骤 1 : 线程概念 首先要理解进程(Process ...
随机推荐
- (转)Maven POM中的各种scope的行为总结
原地址:https://blog.csdn.net/cnweike/article/details/52221410 compile:默认的scope.任何定义在compile scope下的依赖将会 ...
- JS高级. 03 混入式继承/原型继承/经典继承、拓展内置对象、原型链、创建函数的方式、arguments、eval、静态成员、实例成员、instanceof/是否在同一个原型链
继承:当前对象没有的属性和方法,别人有,拿来给自己用,就是继承 1 混入式继承 var I={ }; var obj = { name: 'jack', age:18, sayGoodbye : fu ...
- MVC中Model 的Key值不建议用非int型
一次在开发中,key的值用了 byte型,结果插入第一条正常,第二条开始就出错,原因是用byte类型无法实现自动增加1,所以为了方便,建议使用 int型. public virtual byte bk ...
- 1139: [POI2009]Wie
1139: [POI2009]Wie https://www.lydsy.com/JudgeOnline/problem.php?id=1139 分析: Dijkstra.状压最短路,dis[i][j ...
- LeetCode:18. 4Sum(Medium)
1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...
- 导入execl到数据库mysql
GwykhrenyuankuList <body jwcid="$content$"> <span jwcid="@components/AppBord ...
- 【连载】Bootstrap开发漂亮的前端界面之插件开发
相关文章: 1.<教你用Bootstrap开发漂亮的前端界面> 2.<Bootstrap开发漂亮的前端界面之实现原理> 3.<Bootstrap开发漂亮的前端界面之自定义 ...
- 一个关于sql更新的小笔记
一直在sqlserver下写东西,突然用mysql有些语法发生了改变,有点折腾 (MS SQL Server)语句:update A set a.Name = b.Name from A ...
- 如何激活win10
第一步:用管理员权限打开命令提示符: 第二步:输入命令---slmgr.vbs /upk (成功卸载了产品密钥) 第三步:slmgr /ipk NPP ...
- 异常处理中try,else,finally含有return的情况解析
直接看代码,拿到你的py下运行测试一下就 明白了. 例一: def f(): try: print() finally: print() print(f()) # 若注释掉finally内的retur ...