多线程——————停止线程

下面我们来讲一下:多线程——————停止线程

停止线程的主要方法:

  • Thread.interrupt():不是中断线程,而是通知线程【应该中断了】,具体到底是否中断还是运行由用户判断决定
  • interrupted():测试当前线程是否中断。线程的中断状态由该方法清除,如果连续两次调用该方法,第二次调用会返回flase
  • isInterrupted():也是测试线程是否中断,但不会清除状态

注意点:

1.如果在sleep()状态下停止某一线程,会进入catch语句,并清除停止状态值,变为flase
2.停止线程再遇见sleep(),也会进入cathch语句
3.线程停止后面语句是否执行?
4.使用return停止线程

下面我们来看例子:

public class test4 extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 500000; i++) {
            System.out.println("i="+(i+1));
        }
    }

    public static void main(String[] args) {
        test4 t=new test4();
        t.start();
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            System.out.println("main catch");
            e.printStackTrace();
        }
        //停止当前线程
        t.interrupt();
    }
}
//通知线程应该中断,是否中断由用户决定


我们发现实际上线程并未停止

public class test5 extends Thread {


    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("i="+(i+1));
        }
    }

    public static void main(String[] args) {

        try {
            test5 t=new test5();
            t.start();
            Thread.sleep(1000);
            System.out.println("是否停止1?="+t.interrupted());
            System.out.println("是否停止2?="+t.interrupted());
        } catch (InterruptedException e) {
            System.out.println("main catch");
            e.printStackTrace();
        }

        System.out.println("-----------------------------------");
        //main 线程
        Thread.currentThread().interrupt();
        System.out.println("是否停止1?="+Thread.interrupted());
        System.out.println("是否停止2?="+Thread.interrupted());
        System.out.println("end");

    }
}
//interrupted会清除线程状态

这里写图片描述

public class test6 extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 500000; i++) {
            if(this.interrupted()){
                System.out.println("已经是停止状态");
                break;
            }
            System.out.println("i="+(i+1));
        }
    }

    public static void main(String[] args) {

        try {
            test6 t=new test6();
            t.start();
            Thread.sleep(2000);
            t.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end");

    }
}
//判断后中断

这里写图片描述

public class test7_0 extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 500000; i++) {
            if(this.interrupted()){
                System.out.println("已经是停止状态,我要退出了");
                break;
            }
            System.out.println("i="+(i+1));
        }
        System.out.println("我要退出了,如果此代码是for又继续运行,线程并未停止");
    }

    public static void main(String[] args) {
        try {
            test7_0 t = new test7_0();
            t.start();
            Thread.sleep(2000);
            t.interrupt();
        } catch (Exception e) {
        }
    }
}
//虽然已经中断了线程,但是for循环外后面的语句正常执行

这里写图片描述

public class test7  extends Thread {

    @Override
    public void 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 (Exception e) {
            System.out.println("进run方法中的catch中了");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            test7 t = new test7();
            t.start();
            Thread.sleep(2000);
            t.interrupt();
        } catch (Exception e) {
            System.out.println("main catch");
            e.printStackTrace();
        }
        System.out.println("end");
    }
}
//抛出异常,直接进入catch块

这里写图片描述

public class test8 extends Thread {

    @Override
    public void run() {

        try {
            System.out.println("run begin");
            Thread.sleep(200000);
            System.out.println("run end");
        } catch (InterruptedException e) {
            System.out.println("在沉睡中被停止!进入catch    "+this.isInterrupted());
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            test8 t = new test8();
            t.start();
            Thread.sleep(200);
            t.interrupt();
        } catch (Exception e) {
            System.out.println("main catch");
            e.printStackTrace();
        }
        System.out.println("end");
    }
}
// 如果sleep状态下停止某一个线程,会进入catch语句, 并清除停止状态值,使之变为flase

这里写图片描述

扫描二维码关注公众号,回复: 1699438 查看本文章
public class test9 extends Thread {

    @Override
    public void run() {
        try {
            for (int i = 0; i < 500000; i++) {
                System.out.println("i=" + (i + 1));
            }
            System.out.println("run begin");
            Thread.sleep(200000);
            System.out.println("run end");
        } catch (Exception e) {
            System.out.println("先停止,在遇到了sleep!进入catch");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        test9 t=new test9();
        t.start();
        t.interrupt();
        System.out.println("end");
    }
}

// 先停止线程,再遇见sleep! 也会进入catch语句

这里写图片描述

public class test1 extends Thread {

    @Override
    public void run() {
        while (true) {
            if (this.isInterrupted()) {
                System.out.println("停止了");
                return;
            }
            System.out.println("timer=" + System.currentTimeMillis());
        }
    }

    public static void main(String[] args) {
        try {
            test1 t = new test1();
            t.start();
            Thread.sleep(2000);
            t.interrupt();
        } catch (Exception e) {

        }
    }
}
//不过还是建议使用"抛异常"的方法来实现线程的停止,因为catch块中还可以将异常向上抛 使线程停止的事件得以传播

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq847540343/article/details/73838459