日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多线程:中断(interrupt、interrupted、executor)

發布時間:2025/3/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多线程:中断(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 end

Executor 的中斷操作

調用 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。