• java以正确的方式停止线程


    java线程停止可以说是非常有讲究的,看起来非常简单,但是也要做好一些防范措施,一般停止一个线程可以使用Thread.stop();来实现,但是最好不要用,因为他是不安全的。

    大多数停止线程使用Thread.interrupt()方法,但是这个方法不会终止一个线程,还需要加入一个判断才可以完成线程的停止。

    下面介绍一下常用停止线程应该使用的方法:

    1、interrupt()

      Thread.interrupt()方法仅仅设置线程的状态位为中断线程,并不是让线程马上停止,中断线程之后会抛出interruptException异常。

    2、Thread.interrupted()

      测试当前线程是否已经是中断状态,并且清楚状态。Boolean类型,中断返回true,反之false。当前线程指的是当前方法,并不是启动当前方法的线程。而清楚状态,则是指如果调用两次interrupted()方法,则第二次返回false。

    3、Thread.isInterrupted()

      测试线程是否已经是是中断状态,但是不清除状态标志。

    其实上面的开始我也不理解,经过多次思考,我用自己的话总结一下吧。

    1、interrupt()方法仅仅将线程设置为中断状态,但是并不会去停止线程,返回true说明线程的中断状态已经被设置了。

    2、

    interrupt()、interrupted()和isInterrupted()的具体使用:

    interrupt()、interrupted()使用:

    public class Test01 extends Thread {
    
        @Override
        synchronized public void run() {
            super.run();
            for (int i = 0; i < 100000; i++) {
                //判断线程是否停止
                if (this.interrupted()){
                    System.out.println("已经停止了");
                    break;
                }
                System.out.println(i);
            }
            System.out.println("虽然线程已经停止了,但是还是会跳出for循环继续向下执行的");
        }
    
        public static void main(String[] args) {
            Test01 test = new Test01();
            try {
                test.start();
                test.sleep(50);
                //线程停止
                test.interrupt();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println();
    
        }
    
    }

    打印结果:

    3707
    3708
    3709
    3710
    3711
    
    已经停止了
    虽然线程已经停止了,但是还是会跳出for循环继续向下执行的

    使用异常捕获法停止多线程:

    当interrupted()检测到线程状态为停止的时候,会抛出异常,继而捕获这个异常来停止多线程

    /**
     * 使用异常捕获法停止多线程
     */
    public class Test01 extends Thread {
    
        @Override
        synchronized public void run() {
            super.run();
            try {
                for (int i = 0; i < 100000; i++) {
                    //判断线程是否停止
                    if (this.interrupted()) {
                        System.out.println("已经停止了");
                        //抛出异常
                        throw new InterruptedException();
                    }
                    System.out.println(i);
                }
            } catch (InterruptedException e) {
                System.out.println("线程结束...");
            }
        }
    
        public static void main(String[] args) {
            Test01 test = new Test01();
            try {
                test.start();
                test.sleep(100);
                test.interrupt();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println();
    
        }
    
    }

     打印语句:

    8219
    8220
    8221
    8222
    8223
    8224
    
    已经停止了
    线程结束...
  • 相关阅读:
    Objective-C 复合
    useContext的使用
    context的使用
    redux使用(二)
    redux使用(一)
    React class & function component 的区别
    combineReducers使用
    gnvm使用(未使用成功)
    React相关知识点
    eslint简单使用&&eslint与webpack结合使用
  • 原文地址:https://www.cnblogs.com/itiande/p/9529272.html
Copyright © 2020-2023  润新知