多线程:中断(interrupt、interrupted、executor)
一個(gè)線程執(zhí)行完畢之后會自動結(jié)束,如果在運(yùn)行過程中發(fā)生異常也會提前結(jié)束。
InterruptedException
通過調(diào)用一個(gè)線程的 interrupt() 來中斷該線程,如果該線程處于阻塞、限期等待或者無限期等待狀態(tài),那么就會拋出 InterruptedException,從而提前結(jié)束該線程。但是不能中斷 I/O 阻塞和 synchronized 鎖阻塞。
對于以下代碼,在 main() 中啟動一個(gè)線程之后再中斷它,由于線程中調(diào)用了 Thread.sleep() 方法,因此會拋出一個(gè) InterruptedException,從而提前結(jié)束線程,不執(zhí)行之后的語句。
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()
如果一個(gè)線程的 run() 方法執(zhí)行一個(gè)無限循環(huán),并且沒有執(zhí)行 sleep() 等會拋出 InterruptedException 的操作,那么調(diào)用線程的 interrupt() 方法就無法使線程提前結(jié)束。
但是調(diào)用 interrupt() 方法會設(shè)置線程的中斷標(biāo)記,此時(shí)調(diào)用 interrupted() 方法會返回 true。因此可以在循環(huán)體中使用 interrupted() 方法來判斷線程是否處于中斷狀態(tài),從而提前結(jié)束線程。
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 的中斷操作
調(diào)用 Executor 的 shutdown() 方法會等待線程都執(zhí)行完畢之后再關(guān)閉,但是如果調(diào)用的是 shutdownNow() 方法,則相當(dāng)于調(diào)用每個(gè)線程的 interrupt() 方法。
以下使用 Lambda 創(chuàng)建線程,相當(dāng)于創(chuàng)建了一個(gè)匿名內(nèi)部線程。
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 中的一個(gè)線程,可以通過使用 submit() 方法來提交一個(gè)線程,它會返回一個(gè) Future<?> 對象,通過調(diào)用該對象的 cancel(true) 方法就可以中斷線程。
Future<?> future = executorService.submit(() -> {// .. }); future.cancel(true);總結(jié)
以上是生活随笔為你收集整理的多线程:中断(interrupt、interrupted、executor)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 操作系统:优先级反转
- 下一篇: 多线程:线程之间的协作(join、wai