【Java】结束线程

1. 使用标志位

public class ThreadEx implements Runnable {
    private volatile boolean isStoped = false;

    public void run() {
        while (isStoped) {
            // do something
        }
    }

    public void stop {
        isStoped = true;
    }
}

上面这种方法当启动线程后在关闭时需要手动调用stop方法关闭线程,如果程序中只有一个线程后台运行,那么可以采用以下方式保证程序重新启动时,正确关闭上一次打开的后台线程,避免内存溢出。

public class Handler {
    private static ThreadEx t;

    public static void start() {
        if (t != null)
            t.stop();

        t = new ThreadEx();
        new Thread(t).satrt();
    }
}

2. 使用中断

public class InterruptedExample {

    public static void main(String[] args) throws Exception {
        InterruptedExample interruptedExample = new InterruptedExample();
        interruptedExample.start();
    }

    public void start() {
        MyThread myThread = new MyThread();
        myThread.start();

        try {
            Thread.sleep(3000);
            myThread.cancel();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private class ThreadEx extends Thread{

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("interrupt");
                    // 注意异常处理
                    Thread.currentThread().interrupt();
                }
            }
            System.out.println("stop");
        }

        public void stop(){
            interrupt();
        }
    }
}

对线程调用interrupt()方法,不会真正中断正在运行的线程,只是发出一个请求,由线程在合适时候结束自己。正常情况下,线程接收到interrupt会在下一次判断中跳出循环,但是如果线程处于挂起状态,此时收到interrupt请求就会抛出异常,抛出InterruptedException后,中断标记会被重新设置为false,所以在InterruptedExample例子里,在接收到中断请求时,标准做法是执行Thread.currentThread().interrupt()恢复中断,让线程退出。

参考

http://www.jianshu.com/p/536b0df1fd55
http://blog.csdn.net/xplee0576/article/details/45133791

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值