一般来说线程执行完run()之后就自动结束了,不过有些时候我们需要线程不停的做一些事情,也就是使用while循环,那么这时候该如何停止线程呢?
这个问题需要分情况来讨论,如果线程做的事情不是耗时的,那么只需要使用一个标志即可,具体的代码如下:
如果需要退出时,调用setStop()即可。这里使用了一个Java关键字volatile,这个关键字的目的是如果修改了isStop的值,那么在while循环中可以立即读取到修改后的值,关于volatile的详细介绍可以参考我的另一篇博文 java线程同步volatile与synchronized 。
如果线程做的事情是耗时或者说阻塞的(如调用了sleep,同步锁的wait,socket的receiver,accept等方法),那么就需要用到interrupt()了,调用该函数时会抛出InterruptedException异常,代码中通过捕获该异常,然后break出循环,就可以了。代码如下:
这里既然说到了interrupt(),那么就不得不提另外两个函数:interrupted()和isInterrupted。看下这两个函数的作用:
public static boolean interrupted()
测试当前线程是否已经中断,并且清除线程的中断状态。
public boolean isInterrupted()
测试线程是否已经中断,线程的中断状态不受该方法的影响。
这两个函数的返回值含义:
如果当前线程已经中断,则返回 true;否则返回 false。
其实当线程阻塞并且调用了interrupt()时,不止是抛出InterruptedException异常还会调用interrupted()来清除线程的中断状态,所以在catch里面调用isInterrupted()会返回false。
为了更好的理解这两个函数,我们看个例子:
执行效果如下(每一次执行打印的都不一样,因为没有做线程同步):
...
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. 释放该线程所持有的所有的锁。
好了,关于线程如何停止的问题到这里就圆满收官了,大家有什么问题欢迎留言讨论。
这个问题需要分情况来讨论,如果线程做的事情不是耗时的,那么只需要使用一个标志即可,具体的代码如下:
如果需要退出时,调用setStop()即可。这里使用了一个Java关键字volatile,这个关键字的目的是如果修改了isStop的值,那么在while循环中可以立即读取到修改后的值,关于volatile的详细介绍可以参考我的另一篇博文 java线程同步volatile与synchronized 。
如果线程做的事情是耗时或者说阻塞的(如调用了sleep,同步锁的wait,socket的receiver,accept等方法),那么就需要用到interrupt()了,调用该函数时会抛出InterruptedException异常,代码中通过捕获该异常,然后break出循环,就可以了。代码如下:
这里既然说到了interrupt(),那么就不得不提另外两个函数:interrupted()和isInterrupted。看下这两个函数的作用:
public static boolean interrupted()
测试当前线程是否已经中断,并且清除线程的中断状态。
public boolean isInterrupted()
测试线程是否已经中断,线程的中断状态不受该方法的影响。
这两个函数的返回值含义:
如果当前线程已经中断,则返回 true;否则返回 false。
其实当线程阻塞并且调用了interrupt()时,不止是抛出InterruptedException异常还会调用interrupted()来清除线程的中断状态,所以在catch里面调用isInterrupted()会返回false。
为了更好的理解这两个函数,我们看个例子:
执行效果如下(每一次执行打印的都不一样,因为没有做线程同步):
...
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. 释放该线程所持有的所有的锁。
好了,关于线程如何停止的问题到这里就圆满收官了,大家有什么问题欢迎留言讨论。