Java中如何结束线程

一般来说线程执行完run()之后就自动结束了,不过有些时候我们需要线程不停的做一些事情,也就是使用while循环,那么这时候该如何停止线程呢?
这个问题需要分情况来讨论,如果线程做的事情不是耗时的,那么只需要使用一个标志即可,具体的代码如下:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class MyThread extends Thread {         
  2.     private volatile boolean isStop = false;      
  3.     public void run() {  
  4.         while (!isStop) {  
  5.             System.out.println("do something");  
  6.         }  
  7.     }  
  8.     public void setStop() {  
  9.         isStop = true;  
  10.     }        
  11. }  
如果需要退出时,调用setStop()即可。这里使用了一个Java关键字volatile,这个关键字的目的是如果修改了isStop的值,那么在while循环中可以立即读取到修改后的值,关于volatile的详细介绍可以参考我的另一篇博文 java线程同步volatile与synchronized
如果线程做的事情是耗时或者说阻塞的(如调用了sleep,同步锁的wait,socket的receiver,accept等方法),那么就需要用到interrupt()了,调用该函数时会抛出InterruptedException异常,代码中通过捕获该异常,然后break出循环,就可以了。代码如下:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class MyThread extends Thread {    
  2.     private volatile boolean isStop = false;      
  3.     public void run() {  
  4.         while (!isStop) {  
  5.             try {  
  6.                 System.out.println("do something");  
  7.                 Thread.sleep(1000);  
  8.             } catch (InterruptedException e) {  
  9.                 e.printStackTrace();  
  10.                 break;//如果没有这一句,而且不调用setStop(),线程并不会结束,需要特别注意一下  
  11.             }  
  12.         }  
  13.     }  
  14.     public void setStop() {  
  15.         isStop = true;  
  16.     }        
  17. }    
  18. public class Tes1 {  
  19.     public static void main(String[] args) {    
  20.         MyThread t1 = new MyThread();  
  21.         t1.start();  
  22.         try {  
  23.             Thread.sleep(10);  
  24.             t1.setStop();  
  25.             t1.interrupt();  
  26.         } catch (InterruptedException e) {  
  27.             // TODO Auto-generated catch block  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31. }  
这里既然说到了interrupt(),那么就不得不提另外两个函数:interrupted()和isInterrupted。看下这两个函数的作用:
public static boolean interrupted() 
测试当前线程是否已经中断,并且清除线程的中断状态。
public boolean isInterrupted() 
测试线程是否已经中断,线程的中断状态不受该方法的影响。 
这两个函数的返回值含义: 
如果当前线程已经中断,则返回 true;否则返回 false。 
其实当线程阻塞并且调用了interrupt()时,不止是抛出InterruptedException异常还会调用interrupted()来清除线程的中断状态,所以在catch里面调用isInterrupted()会返回false。
为了更好的理解这两个函数,我们看个例子:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class MyThread extends Thread {         
  2.     private int counter = 0;      
  3.     public void run() {    
  4.         boolean done = false;    
  5.         try{    
  6.             Thread.sleep(10);  
  7.         }catch(InterruptedException ie){    
  8.             ie.printStackTrace();    
  9.             return;  
  10.         }  
  11.         while (counter < 2000 &&!done) {    
  12.             System.out.println(counter++ + " in thread isInterrupted() "+isInterrupted());  
  13.             if(isInterrupted()==true){    
  14.                 try{  
  15.                     System.out.println("in thread after interrupted() "+isInterrupted());  
  16.                     sleep(100);  
  17.                     break;  
  18.                 }catch(InterruptedException ie){    
  19.                     ie.printStackTrace();  
  20.                     break;  
  21.                 }    
  22.             }    
  23.         }    
  24.     }         
  25. }    
  26. public class Tes1 {  
  27.     public static void main(String[] args) {    
  28.         final MyThread t1 = new MyThread();    
  29.         t1.start();    
  30.         new Timer(true).schedule(new TimerTask() {    
  31.             public void run() {    
  32.                 System.out.println("exec interrupt");    
  33.                 t1.interrupt();  
  34.                 System.out.println("in timer isInterrupted() "+t1.isInterrupted());    
  35.             }    
  36.         }, 20);  
  37.     }    
  38. }  
执行效果如下(每一次执行打印的都不一样,因为没有做线程同步):
...
1300 in thread isInterrupted() false
exec interrupt
in timer isInterrupted() true
1301 in thread isInterrupted() true
in thread after interrupted() true
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.example.wd.MyThread.run(Tes1.java:21)
还可能是这样:
...
1568 in thread isInterrupted() false
exec interrupt
in thread after interrupted() true
in timer isInterrupted() true
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.example.wd.MyThread.run(Tes1.java:21)
由于在sleep时执行了interrupt()所以会抛出异常。
如果把if(isInterrupted()==true)改成if(Thread.interrupted()==true)那么打印的结果就不同了,如下:
...
1582 in thread isInterrupted() false
exec interrupt
in timer isInterrupted() true
in thread after interrupted() false
还可能是这样:
...
1771 in thread isInterrupted() false
exec interrupt
in timer isInterrupted() true
1772 in thread isInterrupted() false
in thread after interrupted() false
也可能是这样:
...
476 in thread isInterrupted() false
in thread after interrupted() false
in timer isInterrupted() false
虽然执行了interrupt(),但是由于Thread.interrupted()可以清除中断,所以并不会抛出异常。
前面我们针对线程是否耗时,给出了停止线程的方法,其实还有一个办法那就是使用thread.stop()来强行终止线程,不过这个由于该方法不安全已经废弃掉了,因为他有下面两个缺陷:
1. 立即抛出ThreadDeath异常,在线程的run()方法内,任何一点都有可能抛出ThreadDeath异常,包括在catch或finally语句中。
2. 释放该线程所持有的所有的锁。
好了,关于线程如何停止的问题到这里就圆满收官了,大家有什么问题欢迎留言讨论。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值