栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java-日常练习-多线程-停止线程方法介绍(stop,interrupt,结合while使用退出标签)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java-日常练习-多线程-停止线程方法介绍(stop,interrupt,结合while使用退出标签)


1.姿势不对interrupt 永远不能停止线程运行

package a;
class  Test extends  Thread{
    @Override
    public void run() {
        super.run();
        for (int i = 0; i <1000 ; i++) {
            System.out.println("i="+i);
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Sender extends Thread {

    public static void main(String[] args) throws InterruptedException {
    Test a=new Test();
    a.start();
    a.interrupt();
    Thread.sleep(100);
    System.out.println("已经让你停止了");

    }
}

从打印结果来看, 并没有停止呀, 大家知道什么原因吗?

诊断没有停止的原因
先看诊断停止的方法的区别

public class Sender extends Thread {

    public static void main(String[] args) throws InterruptedException {

    Thread.currentThread().interrupt();

    System.out.println(Thread.currentThread().isInterrupted());
    System.out.println(Thread.currentThread().isInterrupted());
    System.out.println(Thread.interrupted());
    System.out.println(Thread.interrupted());
    System.out.println("已经让你停止了");

    }
}

打印的结果:

true
true
true
false
已经让你停止了

大家看到调用了interrupt方法,还是没有停止主线程,所以当调用了 interrupt方法 只是给线程打个flag, 根据flag 进行判断后边的操作,这样更灵活。所以抛弃了stop 这样暴力 不可控的方法。
能停止线程的方法

  1. for循环根据线程标签状态停止
class  Test extends  Thread{
    @Override
    public void run() {
        super.run();

        for (int i = 0; i <100000 ; i++) {
            if(this.isInterrupted()){
                System.out.println("停止成功了");
                break;
            }

            System.out.println("i="+(i+1));

        }
        System.out.println("for循环结束了");

    }
}

public class Sender extends Thread {

    public static void main(String[] args) throws InterruptedException {

    Test a= new Test();
    a.start();
    Thread.sleep(40);
    a.interrupt();
    System.out.println("end");

    }
}

打印结果显示已经中断了

上边的方法缺点: 如果for 或者while 循环下边还有语句,那么程序还会继续运行,上边的日志 for循环结束了就是for循环后边的代码 。
解决方法 直接用 return 尝试下,直接修改break 变成return ,打印结果如下:

虽然可以实现立即中断的效果,大家有没有考虑过, 这样是否太暴力了, 比如我想中断后想处理下数据作为收尾怎么办,可以用异常中断方法。
异常中断方法
请看下边例子 :

package a;
class  Test extends  Thread{
    @Override
    public void run() {
        super.run();
        try {
            for (int i = 0; i <100000 ; i++) {
                if(this.isInterrupted()){
                    System.out.println("停止成功了");

                        throw new InterruptedException();
                    }
                System.out.println("i="+(i+1));
                }

            System.out.println("for循环结束了");
        }
        catch (InterruptedException e) {
            System.out.println("已经触发了异常,我开始清理工作中.......");
            }


        }


    }


public class Sender extends Thread {

    public static void main(String[] args) throws InterruptedException {

    Test a= new Test();
    a.start();
    Thread.sleep(40);
    a.interrupt();
    System.out.println("end");

    }
}

打印结果:

满足了我们所有的需求。

在sleep 状态下停止线程会报异常

class  Test extends  Thread{
    @Override
    public void run() {
        super.run();
        try {
            for (int i = 0; i <100000 ; i++) {
                Thread.sleep(1000);
                if(this.isInterrupted()){
                    System.out.println("停止成功了");

                        throw new InterruptedException();
                    }
                System.out.println("i="+(i+1));
                }

            System.out.println("for循环结束了");
        }
        catch (InterruptedException e) {
            System.out.println("已经触发了异常,我开始清理工作中.......");
            e.printStackTrace();
            }


        }


    }


public class Sender extends Thread {

    public static void main(String[] args) throws InterruptedException {

    Test a= new Test();
    a.start();
    Thread.sleep(40);
    a.interrupt();
    System.out.println("end");

    }
}


总结:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328267.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号