多线程:中断(interrupt、interrupted、executor)
一個線程執行完畢之后會自動結束,如果在運行過程中發生異常也會提前結束。
InterruptedException
通過調用一個線程的 interrupt() 來中斷該線程,如果該線程處于阻塞、限期等待或者無限期等待狀態,那么就會拋出 InterruptedException,從而提前結束該線程。但是不能中斷 I/O 阻塞和 synchronized 鎖阻塞。
對于以下代碼,在 main() 中啟動一個線程之后再中斷它,由于線程中調用了 Thread.sleep() 方法,因此會拋出一個 InterruptedException,從而提前結束線程,不執行之后的語句。
public class InterruptExample {private static class MyThread1 extends Thread {@Overridepublic 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"); } Main run java.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at InterruptExample.lambda$main$0(InterruptExample.java:5)at InterruptExample$$Lambda$1/713338599.run(Unknown Source)at java.lang.Thread.run(Thread.java:745)interrupted()
如果一個線程的 run() 方法執行一個無限循環,并且沒有執行 sleep() 等會拋出 InterruptedException 的操作,那么調用線程的 interrupt() 方法就無法使線程提前結束。
但是調用 interrupt() 方法會設置線程的中斷標記,此時調用 interrupted() 方法會返回 true。因此可以在循環體中使用 interrupted() 方法來判斷線程是否處于中斷狀態,從而提前結束線程。
public class InterruptExample {private static class MyThread2 extends Thread {@Overridepublic 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(); } Thread endExecutor 的中斷操作
調用 Executor 的 shutdown() 方法會等待線程都執行完畢之后再關閉,但是如果調用的是 shutdownNow() 方法,則相當于調用每個線程的 interrupt() 方法。
以下使用 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"); } Main run java.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at ExecutorInterruptExample.lambda$main$0(ExecutorInterruptExample.java:9)at ExecutorInterruptExample$$Lambda$1/1160460865.run(Unknown Source)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)at java.lang.Thread.run(Thread.java:745)如果只想中斷 Executor 中的一個線程,可以通過使用 submit() 方法來提交一個線程,它會返回一個 Future<?> 對象,通過調用該對象的 cancel(true) 方法就可以中斷線程。
Future<?> future = executorService.submit(() -> {// .. }); future.cancel(true);總結
以上是生活随笔為你收集整理的多线程:中断(interrupt、interrupted、executor)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 操作系统:优先级反转
- 下一篇: 多线程:线程之间的协作(join、wai