停止线程的多种方法

来源:互联网 发布:linux怎么写c 编辑:程序博客网 时间:2024/06/04 22:16

例一:stop强制停止线程

public class Thread06 extends Thread {    private int i = 0;    @Override    public void run() {        super.run();        try{            while(true) {                i ++;                System.out.println("i=" + (i + 1));                Thread.sleep(1000);            }        } catch(InterruptedException e) {            e.printStackTrace();        }    }}public static void main(String[] args) {    thread06();}private static void thread06() {    try{        Thread06 thread = new Thread06();        thread.start();        Thread.sleep(9000);        thread.stop();    } catch(InterruptedException e) {        e.printStackTrace();    }}

输出结果

i=2i=3i=4i=5i=6i=7i=8i=9i=10

thread.start();方法启动一个新的线程,该线程与main线程并行运行,main线程睡眠9秒后执行了thread.stop();方法,thread线程被main线程强制停止。

stop方法已被废弃,因为如果强制让线程停止则可能使一些清理的工作得不到完成。另外一个情况就是对锁定的对象进行了“解锁”,导致数据得不到同步的处理,出现数据不一致的结果。

例二:线程睡眠(sleep)时停止(interrupt)线程

public class Thread05 extends Thread {    @Override    public void run() {        super.run();        try{            for(int i = 0; i < 1000; ++ i) {                System.out.println("i="+(i+1));            }            System.out.println("run begin!");            Thread.sleep(200000);            System.out.println("run end!");        } catch (InterruptedException e) {            System.out.println("先停止,再遇到了sleep!进入catch");            e.printStackTrace();        }    }}public static void main(String[] args) {    thread05();}private static void thread05() {    Thread05 thread = new Thread05();    thread.start();    thread.interrupt();    log("end!====================");}

输出结果:

end!====================i=1i=2i=3......i=999i=1000run begin!先停止,再遇到了sleep!进入catchjava.lang.InterruptedException: sleep interrupted    at java.lang.Thread.sleep(Native Method)    at com.qbian.thread.Thread05.run(Thread05.java:12)

线程在睡眠时(sleep)调用它的停止方法(interrupt)会停止线程并抛出InterruptedException异常。

例三、调用interrupt方法停止正在运行的线程,break跳出循环继续执行后续代码

public class Thread02 extends Thread{    @Override    public void run() {        super.run();        for(int i = 0; i < 500000; ++ i) {            if(this.interrupted()) {                System.out.println("已经是停止状态了!我要退出了!");                break;            }            System.out.println("i=" + ( i + 1));        }        System.out.println("我被输出,线程并没有完全停止!");    }}public static void main(String[] args) {    thread02();}private static void thread02() {    try{        Thread02 thread = new Thread02();        thread.start();        Thread.sleep(2000);        thread.interrupt();    } catch(InterruptedException e) {        log("main catch");        e.printStackTrace();    }    log("end!");}

输出结果:

......i=245329i=245330i=245331i=245332i=245333i=245334end!已经是停止状态了!我要退出了!我被输出,线程并没有完全停止!

break只是跳出了for循环,并不会真正的停止线程,run方法内的后续代码会继续执行。

例四、调用interrupt方法停止正在运行的线程,并抛出异常阻止线程后续的代码执行

public class Thread03 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("因为抛出了异常,所以我不会被输出!");        } catch(InterruptedException e) {            System.out.println("进thread03类的run方法中的catch了!");            e.printStackTrace();        }    }}public static void main(String[] args) {    thread03();}private static void thread03() {    try{        Thread03 thread = new Thread03();        thread.start();        Thread.sleep(2000);        thread.interrupt();    } catch(InterruptedException e) {        log("main catch");        e.printStackTrace();    }    log("end!");}

输出结果:

......i=278267i=278268i=278269i=278270i=278271已经是停止状态了!我要退出了!end!进thread03类的run方法中的catch了!java.lang.InterruptedException    at com.qbian.thread.Thread03.run(Thread03.java:11)

主线程调用interrupt方法停止一个正在运行的线程,在被停止的线程内部判断自身状态是否已经停止this.interrupted(),停止的话就抛出异常,可以阻止后续代码的执行,直接进入catch块内。

例五、使用interrupt和return停止线程并阻止后续代码执行

public class Thread07 extends Thread{    @Override    public void run() {        super.run();        while(true) {            if(this.isInterrupted()) {                System.out.println("停止了!后面的时间将不会被输出!");                return;            }            System.out.println("timer=" + System.currentTimeMillis());        }    }}public static void main(String[] args) {    thread07();}private static void thread07() {    try {        Thread07 thread = new Thread07();        thread.start();        Thread.sleep(2000);        thread.interrupt();    } catch (InterruptedException e) {        log("main catch");        e.printStackTrace();    }}

输出结果:

......timer=1496420116082timer=1496420116082timer=1496420116082停止了!后面的时间将不会被输出!

推荐使用抛出异常的方式停止线程执行,因为在catch块中还可以将异常向上抛,使线程停止的事件得到传播。