停止线程线程是多线程开发时很重要的技术点,掌握此技术可以对线程的停止进行有效的处理。停止线程在java语言中并不像break语句那边干脆,需要一些技巧处理。
停止一个线程意味着在线程处理完任务之前停掉正在做的操作,也就是放弃当前的操作。虽然看起来很简单,但是必须做好防范性措施,以便达到预期的效果。停止一个线程可以使用Thread.stop()方法,但是最好不要使用它。虽然它可以停止一个正在运行的线程,但是这个方法是不安全的,而且已经被弃用作废了。
大多数停止一个线程的操作使用Thread.interrupt()方法,尽管方法的名称是“停止”、“终止”的意思,但是这个方法不会终止一个正在运行的线程,还需要加入一个判断才可以完成线程的停止。这个在后续也会进行介绍。
在Java中有以下3中方式可以终止正在运行的线程:
- 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。
- 使用stop方法强行终止线程,但是不推荐使用这个方法,因为stop和suspend及resume一样,都是作废过期的方法,使用它们可能产生不可预料的结果。
使用interrupt方法中断线程。 这三种方法在后面都会进行介绍。
停不了的线程
本示例使用interrupt()方法来停止线程,但interrupt()方法的使用效果并不像for+break语句那样,马上停止循环。调用interrupt()方法仅仅实在当前线程打一个停止的标记,并不是真的停止线程。示例代码如下:
public class MyThread extends Thread{
@Override
public void run() {
for(int i=0;i<1000;i++){
System.out.println("i="+i);
}
}
}
public class Run {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
try {
t.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();
}
}
代码运行结果如下:
从运行结果来看调用interrupt方法并没有停止线程。
判断线程是否停止状态:
在介绍如何停止线程的前,先看一下如何判断线程的状态是否停止的。在JDK中,Therad类提供了两种方法:
- this.interrupted():测试当前线程是否已经中断。
- this.isInterrupted():测试线程是否已经中断。
interrupted()方法的声明如下所示:
public static boolean interrupted()
isInterrupted()方法的声明如下所示:
public boolean isInterrupted()
那么这两个方法有什么区别呢?先看一下this.interrupted()方法的解释:测试当前线程是否已经中断,当前线程是指运行this.interrupted()方法的线程。为了更深入的了解这个方法,创建如下类:
public class MyThread extends Thread{
@Override
public void run() {
for(int i=0;i<1000;i++){
System.out.println("i="+i);
}
}
}
public class Run {
public static void main(String[] args) {
try {
MyThread t = new MyThread();
t.start();
t.sleep(1000);
t.interrupt();
System.out.println("是否停止1?="+t.interrupted());
System.out.println("是否停止2?="+t.interrupted());
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end");
}
}
程序运行结果如下图:
类Run中虽然通过t对象调用以下代码:
t.interrupt();
来停止thread对象所代表的线程,在后面代码中通过:
System.out.println("是否停止1?="+t.interrupted());
System.out.println("是否停止2?="+t.interrupted());
来判断t对象所代表线程是否终止,但从控制台打印结果看,线程并未停止,这也证明interrupted()方法的解释:测试当前线程是否已经中断。这个“当前线程”是main,它从未中断过,所以打印结果都为false。
如何使main线程产生中断效果呢,创建下面这个类:
public class Run2 {
public static void main(String[] args) {
Thread.currentThread().interrupt();
System.out.println("是否停止?="+Thread.interrupted());
System.out.println("是否停止?="+Thread.interrupted());
System.out.println("end");
}
}
程序运行结果如下图:
从结果来看,方法interrupted()的确判断出当前线程是否是停止状态。但为什么第2个布尔值是false呢,通过查看官方文档对interrupted方法的解释:
测试当前线程是否已中断。线程的中断状态由该方法清除。换句话说,如果连续两次调用该方法,则第二次调用将返回false(在第一次调用时已清除了其中中断状态之后,第二次调用是中断前状态,当前线程再次中断的情况除外)。
文档解释的很清楚了,interrupted()方法具有清楚状态的功能,所以第二次调用interrupted()方法返回的值是false。
介绍完interrupted()方法后再来看isInterrupted()方法。申明如下:
public boolean isInterrupted()
从声明中可以看出isInterrupted()方法不是static的。
创建如下代码:
public class Run3 {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
thread.interrupt();
System.out.println("是否停止?="+thread.isInterrupted());
System.out.println("是否停止?="+thread.isInterrupted());
System.out.println("end");
}
}
执行效果如图:
从程序结果可以看出,方法isInterrupted()并未清除状态标志,所以打印两个true。
最后,再来看一下这两个方法的解释:
- this.interrupted():测试当前线程是否已经是中断状态,执行后具有将状态标识清除为false的功能。
this.isInterrupted():测试线程Thread对象是否已经是中断状态,但不清除状态标志。
能停止的线程-异常法:
通过上面的方法,我们可以在线程中用for语句来判断一下线程是否是停止状态,如果是停止状态,则后面的代码不再运行即可。创建如下示例代码:
public class MyThread extends Thread{
@Override
public void run() {
for(int i=0;i<500000;i++){
if(this.isInterrupted()){
System.out.println("已经停止了,我要退出了");
break;
}
System.out.println("i="+i);
}
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
t.sleep(1000);
t.interrupt();
System.out.println("end");
}
}
程序运行结果如下:
上面的示例虽然停止了线程,但是如果for循环后面还有语句,还是会继续执行的。创建如下代码:
public class MyThread2 extends Thread{
@Override
public void run() {
for(int i=0;i<500000;i++){
if(this.isInterrupted()){
System.out.println("线程已经是停止状态了,我要退出了");
break;
}
System.out.println("i="+i);
}
System.out.println("我是for循环外的代码,线程并未停止");
}
}
public class Run2 {
public static void main(String[] args) {
try{
MyThread2 m = new MyThread2();
m.start();
Thread.sleep(1000);
m.interrupt();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("end");
}
}
执行结果如下:
该如何解决语句继续运行的问题呢,看下面更新后的代码:
public class MyThread2 extends Thread{
@Override
public void run() {
try{
for(int i=0;i<500000;i++){
if(this.isInterrupted()){
System.out.println("线程已经是停止状态了,我要退出了");
throw new InterruptedException();
}
System.out.println("i="+i);
}
System.out.println("我在for下面");
}catch(Exception e){
System.out.println("进入MyThread2方法的catche块中");
e.printStackTrace();
}
}
}
public class Run2 {
public static void main(String[] args) {
try{
MyThread2 m = new MyThread2();
m.start();
Thread.sleep(1000);
m.interrupt();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("end");
}
}
运行结果如下图:
在沉睡中停止
如果线程在sleep()状态下停止线程,会是什么效果?
创建下面的示例代码:
public class MyThread extends Thread{
@Override
public void run() {
try {
System.out.println("run begin");
Thread.sleep(2000);
System.out.println("run end");
} catch (InterruptedException e) {
System.out.println("在沉睡中被停止!进入catch!"+this.isInterrupted());
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
Thread.sleep(2000);
t.interrupt();
System.out.println("end");
}
}
运行结果如下:
通过运行结果可以看出,如果在sleep状态下停止某一线程,会进入catch语句,并且清除停止状态值,使之变成false。
前一个实验室sleep后interrupt()停止,与之相反的操作是什么效果。代码如下:
public class MyThread1 extends Thread{
@Override
public void run() {
try {
for(int i=0;i<500000;i++){
System.out.println("i="+i);
}
System.out.println("run begin");
Thread.sleep(1000);
System.out.println("run end");
} catch (InterruptedException e) {
System.out.println("先停止,再遇到了sleep!进入catche!");
e.printStackTrace();
}
}
}
public class Run2 {
public static void main(String[] args) {
MyThread1 t = new MyThread1();
t.start();
t.interrupt();
System.out.println("end!");
}
}
能停止的线程-暴力停止:
使用stop方法停止线程是非常暴力的,示例代码如下:
public class MyThread extends Thread {
private int i = 0;
@Override
public void run() {
try {
while (true) {
i++;
System.out.println("i=" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
Thread.sleep(8000);
t.stop();
}
}
执行结果如下:
方法stop()与java.lang.ThreadDeath异常
调用stop()方法时会抛出java.lang.ThreadDeath异常,但在通常的情况下,此异常不需要显示地捕捉。测试代码如下:
public class MyThread2 extends Thread{
@Override
public void run() {
try{
this.stop();
}catch(ThreadDeath e){
System.out.println("进入了catch方法");
e.printStackTrace();
}
}
}
public class Run2 {
public static void main(String[] args) {
MyThread2 t = new MyThread2();
t.start();
}
}
运行结果如下:
方法stop()已经作废,因为如果强制让线程停止则有可能使一些清理工作得不到完成。另外一个情况就是对锁定的对象进行“解锁”,导致数据得不到同步的处理,出现数据不一致的情况。
释放锁的不良后果
使用stop()释放锁将会给数据造成不一致的结果。如果出现这样的情况,程序处理的数据就可能遭到破坏,最终导致程序执行的流程错误,一定要特别注意。示例代码如下:
public class SynchronizedObject {
private String username = "a";
private String password = "aa";
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
synchronized public void printString(String username,String password){
try {
this.username = username;
Thread.sleep(100000);
this.password = password;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class MyThread extends Thread{
private SynchronizedObject object;
public MyThread(SynchronizedObject object){
this.object = object;
}
@Override
public void run() {
object.printString("b", "bb");
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
SynchronizedObject object = new SynchronizedObject();
MyThread thread = new MyThread(object);
thread.start();
Thread.sleep(500);
thread.stop();
System.out.println(object.getUsername()+" " + object.getPassword());
}
}
执行结果如下:
这就是stop()方法带来的危害,强烈不建议是stop()来停止线程。
使用return停止线程
将方法interrupt()与return结合使用也能实现停止线程的效果。示例代码如下:
public class MyThread extends Thread{
@Override
public void run() {
while(true){
if(this.isInterrupted()){
System.out.println("停止了!");
return;
}
System.out.println("time="+System.currentTimeMillis());
}
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
Thread.sleep(1000);
t.interrupt();
System.out.println("end");
}
}
执行结果: