中斷(interrupted()、isInterrupted())、Executor的中斷

1. 中斷

一個線程執行完畢之後會自動結束,如果在運行過程中發生異常也會提前結束。

InterruptedException通過調用一個線程的 interrupt() 來中斷該線程,如果該線程處於阻塞、限期等待或者無限期等待狀態,那麼就會拋出 InterruptedException,從而提前結束該線程。

但是不能中斷 I/O 阻塞和 synchronized 鎖阻塞。

對於以下代碼,在 main() 中啓動一個線程之後再中斷它,由於線程中調用了Thread.sleep() 方法,因此會拋出一個InterruptedException,從而提前結束線程,不執行之後的語句。

public class InterruptExample {
    private static class MyThread1 extends Thread {
        @Override
        public void run() {
            try {
                Thread.sleep(2000);
                System.out.println("Thread run");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
} 

public static void main(String[] args) throws InterruptedException {
    Thread thread1 = new MyThread1();
    thread1.start();
    thread1.interrupt();
    System.out.println("Main run");
}

結果:

1.1 interrupted()

如果一個線程的 run() 方法執行一個無限循環,並且沒有執行 sleep() 等會拋出InterruptedException 的操作,那麼調用線程的 interrupt() 方法就無法使線程提前結束。

由於調用 interrupt() 方法會設置線程的中斷標記,此時調用 interrupted() 方法會返回 true。因此可以在循環體中使用 interrupted() 方法來判斷線程是否處於中斷狀態,從而提前結束線程。

public class InterruptExample {
    private static class MyThread2 extends Thread {
        @Override
        public void run() {
            while (!interrupted()) {
                // ..
            } 
            System.out.println("Thread end");
        }
    }
} 

public static void main(String[] args) throws InterruptedException {
    Thread thread2 = new MyThread2();
    thread2.start();
    thread2.interrupt();
}

1.1.1 interrupted()源碼(原博

interrupted()是靜態方法:內部實現是調用的當前線程的isInterrupted(),並且會重置當前線程的中斷狀態。

1.1.2 isInterrupted()源碼

 isInterrupted()是實例方法,是調用該方法的對象所表示的那個線程的isInterrupted(),不會重置當前線程的中斷狀態

1.1.3 測試對比

第一個紅框中斷的線程是我們自己創建的myThread線程,我調用的interrupted(),由上面源碼可知是判斷當前線程的中斷狀態,當前線程是main線程,我根本沒有中斷過main線程,所以2次調用均返回“false”

第一個紅框中斷的線程是當前線程(main線程),我調用的interrupted(),由上面源碼可知是判斷當前線程的中斷狀態,當前線程是main線程,所以第1次調用結果返回“true”,因爲我確實中斷了main線程;

由源碼可知interrupted()調用的是isInterrupted(),並會重置中斷狀態,所以第一次調用之後把中斷狀態給重置了,從中斷狀態重置爲非中斷狀態,所以第2次調用的結果返回“false”。

第一個紅框中斷的線程是我們自己創建的myThread線程,我調用的isInterrupted(),由上面源碼可知是判斷執行該方法的對象所表示線程的中斷狀態,也就是myThread引用所表示的線程的中斷狀態,所以第1次調用結果返回“true”;

由源碼可知isInterrupted()不會重置中斷狀態,所以第一次調用之後沒有把中斷狀態給重置(從中斷狀態重置爲非中斷狀態),所以第2次調用的結果還返回“true”。

第一個紅框中斷的線程是我們自己創建的myThread線程,我調用的isInterrupted(),由上面源碼可知是判斷執行該方法的對象所表示線程的中斷狀態,也就是main的中斷狀態,我壓根沒有中斷main線程,所以理所當然2次調用結果都返回“false”。

第一個紅框中斷的線程是當前線程(main線程),我調用的isInterrupted(),由上面源碼可知是判斷執行該方法的對象所表示線程的中斷狀態,也就是main的中斷狀態,所以第1次調用結果返回“true”;

因爲源碼內部調用isInterrupted() 參數傳的false,不會重置main線程的中斷狀態,所以第2次調用還是返回”true”。

2. Executor 的中斷操作

調用 Executor 的 shutdown() 方法會等待線程都執行完畢之後再關閉;

但是如果調用的是 shutdownNow() 方法,則相當於調用每個線程的 interrupt() 方法。

2.1 舉例

以下使用 Lambda 創建線程,相當於創建了一個匿名內部線程。

public static void main(String[] args) {
    ExecutorService executorService = Executors.newCachedThreadPool();
    executorService.execute(() -> {
        try {
            Thread.sleep(2000);
            System.out.println("Thread run");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    executorService.shutdownNow();
    System.out.println("Main run");
}

2.2 如果只想中斷 Executor 中的一個線程

可以通過使用 submit() 方法來提交一個線程,它會返回一個 Future<?> 對象,通過調用該對象的 cancel(true) 方法就可以中斷線程。

Future<?> future = executorService.submit(() -> {
    // ..
});
future.cancel(true);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章