java中的线程(2):如何正确停止线程之2种常见停止方式

1.常见停止方式

  • 结束run函数,run中含退出标志位。
  • 使用interrupt()方法中断线程
  • 使用stop方法暴力终止(已经弃用)

2.结束run

复制代码
 1     class TestThread extends Thread{
 2         volatile boolean flag = true;
 3         @Override
 4         public void run() {
 5             while (!flag){
 6                 Log.d(TAG, "running ....");
 7             }
 8             Log.d(TAG, "thread " + getId() + " finished !");
 9             Log.d(TAG, "isAlive " + isAlive());
10         }
11         public void stopThread(){
12             flag = false;
13         }
14     }
15     TestThread testThread;
16     void stopThread(){
17         Log.d(TAG, "stopThread: isAlive : " + testThread.isAlive());
18         Log.d(TAG, "stopThread: isInterrupted : " + testThread.isInterrupted());
19 
20         testThread.stopThread();
21         Log.d(TAG, "stopThread: isAlive : " + testThread.isAlive());
22         Log.d(TAG, "stopThread: isInterrupted : " + testThread.isInterrupted());
23 
24     }
复制代码
  • 调用testThread.stopThread就可以了。
  • 使用场景无限制

3.使用interrupt()方法

3.1示例

复制代码
 1   class TestThread extends Thread{
 2         @Override
 3         public void run() {
 4             while (!Thread.currentThread().isInterrupted()){
 5                 Log.d(TAG, "running .... interrupt = " + this.isInterrupted());
 6                 try {
 7                     Thread.sleep(1000 * 1);
 8                 } catch (InterruptedException e) {
 9                     e.printStackTrace();
10                     Log.d(TAG, "InterruptedException interrupt is reset = " + this.isInterrupted());
11                     // Thread.sleep()方法由于中断抛出异常。
12                     // Java虚拟机会先将该线程的中断标识位清除,然后抛出InterruptedException,
13                     // 因为在发生InterruptedException异常的时候,会清除中断标记
14                     // 如果不加处理,那么下一次循环开始的时候,就无法捕获这个异常。
15                     // 故在异常处理中,再次设置中断标记位
16                     Thread.currentThread().interrupt();
17                 }
18             }
19             Log.d(TAG, "thread " + getId() + " finished !");
20             Log.d(TAG, "isAlive " + isAlive());
21         }
22     }
23     TestThread testThread;
24     void stopThread(){
25         Log.d(TAG, "stopThread: isAlive : " + testThread.isAlive());
26         Log.d(TAG, "stopThread: isInterrupted : " + testThread.isInterrupted());
27 
28         testThread.interrupt();
29         Log.d(TAG, "stopThread: isAlive : " + testThread.isAlive());
30         Log.d(TAG, "stopThread: isInterrupted : " + testThread.isInterrupted());
31 
32     }
复制代码

3.1使用场景 

  适合线程处于阻塞状态,如使用了sleep,同步锁的wait,socket中的receiver,accept等方法时。

  当调用阻塞线程的interrupt()方法时,会抛出InterruptException异常。通过代码捕获该异常,然后跳出循环状态,从而让我们有机会结束这个线程的执行。

  调用interrupt方法后线程并不一定会结束, 只有捕获InterruptedException异常之后跳出循环,才能正常结束run方法。

4.Thread.stop()

  已经弃用

  http://www.cnblogs.com/sjjg/p/7625571.html

  

 

posted @   f9q  阅读(445)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
历史上的今天:
2016-10-04 java 反射工具类
2015-10-04 屏幕尺寸,屏幕分辨率,屏幕密度,各种长宽单位(px,sp,dp,in.pt,mm)
2015-10-04 多设备官方教程(7)如何支持多种语言
2015-10-04 多设备官方教程(6)控制多版本API
2015-10-04 多设备官方教程(5)多屏幕(下)用代码控制运行时各布局应何时显示
2015-10-04 多设备官方教程(4)多屏幕(中)各大小屏幕都有个布局文件,再对它们生成相同的别名,.9图片技术
2015-10-04 多设备官方教程(3)多屏幕(上)用密度不用像素
点击右上角即可分享
微信分享提示