最近想記錄一下,如何優雅的中斷線程任務的方法。歸納的說,有3種方法:html
一、經過thread.interrupt,固然這須要你線程任務類裏,本身寫"是否中斷"的判斷邏輯java
二、經過Future的cancel方法來實現,這也是咱們這裏要測試的安全
三、經過Thead的實例方法stop來中斷線程,固然由於粗暴和不安全已經被廢棄 想的很好,也是我想用代碼實現一下,因而發現了一個問題,就是實用方法2取消線程時,必需要在任務類的run方法中使用Thread.sleep(),否則不會被中斷。廢話不說,上代碼:dom
package com.nipin.datastructor; import java.util.Random; import java.util.concurrent.*; /** * Created by nipin on 16/11/28. * 學習如何優雅的中止一個正在執行的線程 * 主要思路: * 一、經過thread.interrupt,固然這須要你線程任務類裏,本身寫"是否中斷"的判斷邏輯 * 二、經過Future的cancel方法來實現,這也是咱們這裏要測試的 * 三、經過Thead的實例方法stop來中斷線程,固然由於粗暴和不安全已經被廢棄 */ public class ThreadCancelDemo { public static void main(String[] args) { //方法1 /* Thread thread = new Thread(){ @Override public void run() { super.run(); while (!isInterrupted()){ try { Thread.sleep(100l); System.out.println(Thread.currentThread().getName()+" print random :"+ new Random().nextInt()); } catch (InterruptedException e) { e.printStackTrace(); } } } }; thread.start(); Thread.sleep(1000l); thread.interrupt(); */ //方法2 ExecutorService executorService = Executors.newFixedThreadPool(10); Runnable runnable = new Runnable() { @Override public void run() { while (true){ System.out.println(Thread.currentThread().getName()+" print random --> :"+ new Random().nextInt()); } } }; try { Future<?> submit = executorService.submit(runnable); Thread.sleep(100l); boolean cancel = submit.cancel(true); System.out.println("是否已經取消"+cancel); executorService.shutdown(); } catch (InterruptedException e) { e.printStackTrace(); } } }
只要加上Thread.sleep(),以後,就能夠中斷。不理解其中的道理。 看了Future, future.cancel()能夠刪除同步阻塞任務這個帖子後,我恍然大悟,個人任務類裏寫了while(true),這樣的線程只有thead.stop能中斷,其餘的方式只是改變中斷狀態標誌,因此要改成ide
public void run() { while (!Thread.currentThread().isInterrupted()){ System.out.println(Thread.currentThread().getName()+" print random --> :"+ new Random().nextInt()); }
這樣就能夠中斷了。學習