【多线程】——停止线程的三种方式

前提

    停止线程是在多线程开发时非常重要的方式,掌握线程的停止可以对线程的停止进行有效的处理。停止线程在Java中不像break那样干脆,而需要一些技巧性。

停止线程的方式有三种,分别展示一下

方式一

使用退出标识,使得线程正常退出,即当run方法完成后进程终止。

public void run() {
    while(flag){
        //do something
    }
}

利用标识符flag判定线程是否继续执行。

方式二

使用stop强行中断线程(此方法为作废过期方法),不推荐使用,暴力终止,可能使一些清理性的工作得不到完成。还可能对锁定的内容进行解锁,容易造成数据不同步的问题。

方式三

使用interrupt方法中断线程。

在Thread.java类里提供了两种方法判断线程是否为停止的。

this.interrupted():测试当前线程是否已经中断(静态方法)。如果连续调用该方法,则第二次调用将返回false。在api文档中说明interrupted()方法具有清除状态的功能。执行后具有将状态标识清除为false的功能。

this.isInterrupted():测试线程是否已经中断,但是不能清除状态标识。

线程停止——抛异常法

public class MyThread4 extends Thread {
    @Override
    public void run() {
        super.run();
        for (int i = 0; i < 50000; i++) {
            if (this.isInterrupted()) {
                System.out.println( "线程已经结束,我要退出" );
                break;
            }
            System.out.println( "i=" + (i + 1) );
        }
        System.out.println( "我是for下面的语句,我被执行说明线程没有真正结束" );
    }
}
   public static void main(String[] args) {
        try {
            MyThread4 myThread4 = new MyThread4();
            myThread4.start();
            Thread.sleep( 20);
            myThread4.interrupt();
        } catch (InterruptedException e) {
            System.out.println( "main catch" );
            e.printStackTrace();
        }
    }

根据打印结果发现for后面的内容依旧会执行,为了解决这种问题,可以采用抛异常的方式,或return的方式终止线程。一般推荐抛异常的方式,这样才能使得线程停止得以扩散。

public class MyThread4 extends Thread {
    @Override
    public void run() {
        super.run();
        try {
            for (int i = 0; i < 50000; i++) {
                if (this.isInterrupted()) {
                    System.out.println( "线程已经结束,我要退出" );
//                    return;
                    throw new InterruptedException();
                }
                System.out.println( "i=" + (i + 1) );
            }
            System.out.println( "我是for下面的语句,我被执行说明线程没有真正结束" );
        } catch (InterruptedException e) {
            System.out.println( "进入MyThread.java类中run方法的catch异常了" );
            e.printStackTrace();
        }
    }
}

在沉睡中停止

先sleep,后interrupt

  @Override
    public void run() {
        super.run();
        try {
            System.out.println( "begin run" );
            Thread.sleep( 500 );
            System.out.println( "begin end" );
        } catch (InterruptedException e) {
            System.out.println("在沉睡中终止");
            e.printStackTrace();
        }
    }
 public static void main(String[] args) {
        try {
            MyThread5 thread5 = new MyThread5();
            thread5.start();
            Thread.sleep( 20 );
            thread5.interrupt();
        } catch (InterruptedException e) {
            System.out.println( "main catch" );
            e.printStackTrace();
        }
    }

从打印结果看,sleep状态下停止某一个线程,会进入catch语句,并清除状态值,变成false

先interrupt后sleep

  try {
            for (int i = 0; i < 10000; i++) {
                System.out.println( "i="  +(i + 1) );
            }
            System.out.println( "run begin" );
            Thread.sleep( 200 );
            System.out.println( "run end" );
        } catch (InterruptedException e) {
            System.out.println( "先停止,后sleep" );
            e.printStackTrace();
        }
 public static void main(String[] args) {
            MyThread5 thread5 = new MyThread5();
            thread5.start();
            thread5.interrupt();
    }

任务执行完成后,才抛出异常!

总结

    很基础的内容,多加积累!


  • 11
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 25
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mandy_i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值