线程停止总结

2018-12-18  本文已影响0人  xbmchina

1 单线程停止

public class ThreadStopSafeInterrupted {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread() {
            @Override
            public void run() {
                while (true) {
                    // 使用中断机制,来终止线程
                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println("Interrupted ...");
                        break;
                    }
 
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        System.out.println("Interrupted When Sleep ...");
                        // Thread.sleep()方法由于中断抛出异常。
                        // Java虚拟机会先将该线程的中断标识位清除,然后抛出InterruptedException,
                        // 因为在发生InterruptedException异常的时候,会清除中断标记
                        // 如果不加处理,那么下一次循环开始的时候,就无法捕获这个异常。
                        // 故在异常处理中,再次设置中断标记位
                        Thread.currentThread().interrupt();
                    }
 
                }
            }
        };
 
        // 开启线程
        thread.start();
        Thread.sleep(2000);
        thread.interrupt();//主线程发起中断
 
    }
 
}

2 线程池停止

   /**
     * 停止线程池中的所有线程
     */
    private void stopDownloadThreadTask() {
        try {
            this.fixedThreadPool.shutdown();//尝试停止所有线程
            if(!this.fixedThreadPool.awaitTermination(5 * 1000, TimeUnit.MILLISECONDS)){
                this.fixedThreadPool.shutdownNow();//规定时间内还未停止,再次请求停止
            }
        } catch (InterruptedException e) {
            logger.error("awaitTermination interrupted: " + e);
            this.fixedThreadPool.shutdownNow();//停不了就再停止一次。
        }

    }
上一篇 下一篇

猜你喜欢

热点阅读