如何正确的停止一个线程

================================题外话=================================

复习多线程的知识,偶然看到一道题:如何正确的停止一个线程?我第一反应是使用stop()方法停止,操作以后发现stop已经被废弃了。

经过查找资料,发现是使用interrupt()进行中断标记然后在进行停止,特此写这篇随笔记录一下。

------------------------------------------------------------------------------------------------------------------------------------

 

stop()、suspend()的缺点

1.会导致一些清理工作没有进行,导致一些资源没有得到回收,导致内存泄漏;

2.stop()停止线程会导致对锁定的对象进行解锁,在多线程情况下导致数据不一致问题。

 

interrupt()方法

interrupt()方法为为调用该方法的线程对象设置一个中断标志,注意!这里是设置一个中断标志,丝毫不耽误线程没羞没臊的运行。请看例子

 1 public class App {
 2     public static void main(String[] args) {
 3 
 4         try {
 5             MyThread thread = new MyThread();
 6             thread.start();
 7             thread.interrupt();
 8             System.out.println("执行了interrupt方法");
 9         } catch (Exception e) {
10             e.printStackTrace();
11         }
12     }
13 
14 }
15 
16 class MyThread extends Thread {
17     
18     public void run() {
19         try{
20             for(int i=0;i<5000;i++){
21                 System.out.println(i);
22             }
23         }catch(Exception e){
24             e.printStackTrace();
25         }
26     }
27 }

运行结果:

1 ....
2 4992
3 4993
4 4994
5 4995
6 4996
7 4997
8 4998
9 4999

说明在调用了interrupt之后,线程并不会停止。

 

使用interrupted()和isInterrupted()判断中断状态

interrupted()作用于调用该方法所在的线程,而不是调用者所在的线程,即无论哪个线程对象调用interrupted()方法,最后结果都是该方法所在线程的状态结果。

isInterrupted()作用于调用该方法的线程,即谁调用显示谁的中断状态。

并且interrupted()在第二次调用的时候,会恢复线程的状态,即第一次调用如果是中断状态,第二次调用将恢复为非中断状态。

isInterruted()仅仅是查看线程的中断状态,并不会改变状态。请看下面例子

 1 public class App {
 2     public static void main(String[] args) {
 3 
 4         try {
 5             MyThread thread = new MyThread();
 6             thread.start();
 7             thread.interrupt();
 8             
 9             //显示的是主线程的中断状态,所以显示false
10             System.out.println("调用interrupted():  " + thread.interrupted());
11             
12             //main主线程调用interrupt(),是主线程状态为中断
13             Thread.currentThread().interrupt();
14             
15             //这里也可以使用Thread.currentThread().interrupted(),效果是一样的,具体原因看上面介绍
16             System.out.println("第一次调用interrupted():  " + thread.interrupted());
17             //第二次调用,interrupted()方法又将状态恢复
18             System.out.println("第二次调用interrupted():  " + thread.interrupted());
19             
20             System.out.println("-------------------------------------------------");
21             
22             //而isInterrupted()返回的是调用者线程的中断状态,而且多次调用状态不会恢复
23             System.out.println("第一次调用isInterrupted():  " + thread.isInterrupted());
24             System.out.println("第二次调用isInterrupted():  " + thread.isInterrupted());
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28     }
29 }
30 
31 class MyThread extends Thread {
32     
33     public void run() {
34         try{
35             while(true){
36                 
37             }
38         }catch(Exception e){
39             e.printStackTrace();
40         }
41     }
42 }

运行结果为:

1 调用interrupted():  false
2 第一次调用interrupted():  true
3 第二次调用interrupted():  false
4 -------------------------------------------------
5 第一次调用isInterrupted():  true
6 第二次调用isInterrupted():  true

 

注意:如果线程在睡眠状态中,线程状态被修改为interrupted,这是将抛出java.lang.InterruptedException异常,线程将会停止

下面进入正题,如何正确的停止一个线程

有以下几个方式:

使用break停止线程:判断线程状态为中断时,使用break跳出run()方法中的循环,缺点是虽然跳出了循环体,但是循环体外的程序还会得到执行,并不能到达立即停止的效果

使用return停止线程:判断线程状态为中断时,使用return退出run()方法

抛出异常停止线程:通过抛出一个异常来停止线程,这是比较推荐的方式

这三种方式实现基本相似,我就拿异常法进行举例了(偷懒)。

 1 public class App {
 2     public static void main(String[] args) {
 3 
 4         try {
 5             MyThread thread = new MyThread();
 6             
 7             thread.start();
 8             Thread.sleep(2000);
 9             thread.interrupt();
10                         
11             while(true){   //主线程将一直运行
12                 
13             }
14         } catch(Exception e){
15             e.printStackTrace();
16         }
17     }
18 }
19 
20 class MyThread extends Thread {
21     
22     public void run() {
23         try{
24             while(true){
25                 if(isInterrupted()){
26                     throw new InterruptedException();
27                 }
28             }
29         }catch(InterruptedException e){
30             System.out.println("由于异常退出线程");
31             e.printStackTrace();
32         }finally{
33             System.out.println("这是线程的finally");
34         }
35     }
36 }

 

 运行结果:

1 java.lang.InterruptedException
2     at com.sunyong.thread.stop.MyThread.run(App.java:31)
3 由于异常退出线程
4 这是线程的finally

 

转载于:https://www.cnblogs.com/duodushuduokanbao/p/9567433.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
正确停止一个线程,可以通过设置一个标志位,在需要停止线程的时候将标志位设置为True,然后在线程内部不断检查这个标志位,如果发现标志位为True,则退出线程。 以下是一个不停输出信息的线程的示例代码: ```python import threading import time class OutputThread(threading.Thread): def __init__(self): super(OutputThread, self).__init__() self.stopped = False def run(self): while not self.stopped: print("正在计算,请等待...") time.sleep(1) def stop(self): self.stopped = True ``` 接下来,我们可以创建一个模拟计算的线程,当这个线程运行一定时间后,就会停止输出线程。 以下是示例代码: ```python class CalcThread(threading.Thread): def __init__(self, output_thread, duration): super(CalcThread, self).__init__() self.output_thread = output_thread self.duration = duration def run(self): # 模拟计算 time.sleep(self.duration) # 停止输出线程 self.output_thread.stop() ``` 最后,我们可以通过创建一个输出线程一个计算线程,并启动它们来测试我们的代码: ```python if __name__ == '__main__': output_thread = OutputThread() calc_thread = CalcThread(output_thread, 5) output_thread.start() calc_thread.start() calc_thread.join() output_thread.join() print("程序结束") ``` 在这个示例中,我们创建了一个输出线程一个计算线程,输出线程会不断输出"正在计算,请等待...",而计算线程会模拟计算5秒钟后停止输出线程。最后,我们等待计算线程和输出线程运行结束,并输出"程序结束"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值