美文网首页
多线程之(中断线程)

多线程之(中断线程)

作者: 新征程Dream | 来源:发表于2020-07-05 21:28 被阅读0次

    线程中断

    线程中断是门技术活,是我们控制线程死亡的手法。用的好,可以节约资源。当线程满足我们的需求后,想办法中断掉,就可以释放该线程占用的资源,也是一门艺术。Java之前有给我们提供,直接杀死线程的方法,但是过于危险,已经废弃了,不建议使用。但是问题的解决方案永远不止一种。这次,我们来说三种线程中断的解决方案。

    第一种,代码控制。

    我们自己可以设定一个Boolean(布尔)类型的成员变量,通过true和false。当然这种方法可以自由发挥。
    回到代码层面

    package cn.zl.stop;
    public class StopThread implements Runnable {
        // 线程结束的标记
        private boolean flag = false;
        public boolean isFlag() {
            return flag;
        }
        public void setFlag(boolean flag) {
            this.flag = flag;
        }
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println("线程正在执行 " + i);
                if (flag){
                    System.out.println("中断线程");
                    // 直接return掉。让run方法结束执行
                    return;
                }
            }
        }
    }
    

    测试代码

    package cn.zl.stop;
    public class ThreadMain {
        public static void main(String[] args) throws InterruptedException {
            StopThread stopThread = new StopThread();
            Thread thread = new Thread(stopThread);
            thread.start();
            // 让主线程休眠一下,给线程启动提供时间。
            Thread.sleep(2);
            stopThread.setFlag(true);
        }
    }
    

    第二种直接,调用stop方法杀死线程。

    这种方法,已经被抛弃了,但是也算是一种中断线程的方法。
    回到代码层面

    package cn.zl.stop;
    public class ThreadMain {
        public static void main(String[] args) throws InterruptedException {
            StopThread stopThread = new StopThread();
            Thread thread = new Thread(stopThread);
            thread.start();
            // 让主线程休眠一下,给线程启动提供时间。
            Thread.sleep(2);
            thread.stop();
        }
    }
    

    第三种,使用线程内置的中断标识。

    线程内置了一个中断标识,Interrupt。我们可以通过调用这个API,去中断我们的线程。这里分为两种场景,一种是线程处于非休眠状态,另一种是线程处于休眠状态。
    回代码层面
    线程处于非休眠状态

    package cn.zl.stop;
    public class StopThread implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println("线程正在执行 " + i);
                // 获取当前线程的中断标识
                boolean interrupted = Thread.currentThread().isInterrupted();
                if (interrupted){
                    System.out.println("中断线程");
                    // 直接return掉。让run方法结束执行
                    return;
                }
            }
        }
    }
    

    测试一下

    package cn.zl.stop;
    public class ThreadMain {
        public static void main(String[] args) throws InterruptedException {
            StopThread stopThread = new StopThread();
            Thread thread = new Thread(stopThread);
            thread.start();
            // 让主线程休眠一下,给线程启动提供时间。
            Thread.sleep(2);
           // 提醒线程中断
            thread.interrupt();
        }
    }
    

    线程处于休眠状态

    package cn.zl.stop;
    
    public class StopThread implements Runnable {
    
        @Override
        public void run() {
            try {
                // 让线程处于休眠的状态
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 获取当前线程的中断标识
                boolean interrupted = Thread.currentThread().isInterrupted();
                if (interrupted){
                    System.out.println("中断线程");
                    // 直接return掉。让run方法结束执行
                    return;
                }
        }
    }
    

    测试一下

    package cn.zl.stop;
    
    public class ThreadMain {
        public static void main(String[] args) throws InterruptedException {
            StopThread stopThread = new StopThread();
            Thread thread = new Thread(stopThread);
            thread.start();
            // 让主线程休眠一下,给线程启动提供时间。
            Thread.sleep(2);
            // 抛出线程停止的信号
            thread.interrupt();
        }
    }
    

    执行结果


    image.png

    相关文章

      网友评论

          本文标题:多线程之(中断线程)

          本文链接:https://www.haomeiwen.com/subject/ydiuqktx.html