美文网首页
Java 停止线程

Java 停止线程

作者: 凯玲之恋 | 来源:发表于2018-08-16 18:53 被阅读27次

    1概述

    在Java中有以下3种方法可以终止正在运行的线程:

    • 1)当run方法完成后线程终止。
    • 2)使用stop方法强行终止线程,但是不推荐使用这个方法,因为stop和suspend及resume一样,都是作废过期的方法,使用它们可能产生不可预料的结果。
    • 3)使用interrupt方法中断线程。

    2 stop()方法停止线程

        try {
                MyThread thread = new MyThread();
                thread.start();
                Thread.sleep(8000);
                thread.stop();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    • 调用stop()方法时会抛出java.lang.ThreadDeath异常,但在通常的情况下,此异常不需要显式地捕捉。
    • 调用stop()方法后线程会立即停止
      问题
      1. 方法stop()已经被作废,因为如果强制让线程停止则有可能使一些清理性的工作得不到完成。
      1. 另外一个情况就是对锁定的对象进行了“解锁”,导致数据得不到同步的处理,出现数据不一致的问题。

    3 调用interrupt()方法来停止线程

    • 设置线程的中断状态
    • interrupt()方法的使用效果并不像for+break语句那样,马上就停止循环。
    • 调用interrupt()方法仅仅是在当前线程中打了一个停止的标记,并不是真的停止线程

    3.1 this.interrupted() VS this.isInterrupted()

    • this.interrupted():测试当前线程是否已经是中断状态,执行后具有将状态标志置清除为false的功能。
    • this.isInterrupted():测试线程Thread对象是否已经是中断状态,但不清除状态标志。

    3.2 interrupt方法配合抛出异常停止线程

    建议使用“抛异常”的方法来实现线程的停止,因为在catch块中还可以将异常向上抛,使线程停止的事件得以传播。
    【Java 多线程】Java中主线程如何捕获子线程抛出的异常

    public class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                for (int i = 0; i < 500000; i++) {
                    if (this.interrupted()) {
                        System.out.println("已经是停止状态了!我要退出了!");
                        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();
            }
        }
    }
    
    public class Run {
        public static void main(String[] args) {
            try {
                MyThread thread = new MyThread();
                thread.start();
                Thread.sleep(2000);
                thread.interrupt();
            } catch (InterruptedException e) {
                System.out.println("main catch");
                e.printStackTrace();
            }
            System.out.println("end!");
        }
    }
    
    qq_pic_merged_1534415736719.jpg

    3.3 interrupt方法 配合 return 停止线程

    程序正常执行完成,线程退出。

    public class MyThread extends Thread {
        @Override
        public void run() {
                while (true) {
                    if (this.isInterrupted()) {
                        System.out.println("停止了!");
                        return;
                    }
                    System.out.println("timer=" + System.currentTimeMillis());
                }
        }
    }
    
    
    public class Run {
        public static void main(String[] args) throws InterruptedException {
            MyThread t=new MyThread();
            t.start();
            Thread.sleep(2000);
            t.interrupt();
        }
    }
    
    qq_pic_merged_1534415883582.jpg

    3.4 注意

    • 如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException
    • 如果该线程在可中断的通道上的 I/O 操作中受阻,则该通道将被关闭,该线程的中断状态将被设置并且该线程将收到一个ClosedByInterruptException

    参考

    《java多线程编程核心技术》

    相关文章

      网友评论

          本文标题:Java 停止线程

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