java InterruptedException 異常中斷的實現(xiàn)
InterruptedException
當一個線程在被阻塞狀態(tài)(如調(diào)用 Thread.sleep() 或 Object.wait() 方法)時,如果其他線程調(diào)用該被阻塞線程的 interrupt() 方法,那么被阻塞線程會被中斷,并拋出 InterruptedException 異常。
package com.lf.java.basic.concurrent; class MyRunnable implements Runnable { ? ? @Override ? ? public void run() { ? ? ? ? try { ? ? ? ? ? ? // 被阻塞的線程,調(diào)用sleep方法 ? ? ? ? ? ? Thread.sleep(5000); ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? // 被中斷時會拋出InterruptedException異常 ? ? ? ? ? ? System.out.println("Thread was interrupted."); ? ? ? ? ? ? // 可以選擇終止線程的執(zhí)行 ? ? ? ? ? ? // return; ? ? ? ? } ? ? ? ? System.out.println("被喚醒了處理Exception后可以自由選擇做什么事"); ? ? ? ? System.out.println("Thread completed."); ? ? } } public class InterruptedExceptionSample { ? ? public static void main(String[] args) { ? ? ? ? Thread thread = new Thread(new MyRunnable()); ? ? ? ? thread.start(); ? ? ? ? // 主線程休眠一段時間后,中斷被阻塞的線程 ? ? ? ? try { ? ? ? ? ? ? Thread.sleep(2000); ? ? ? ? ? ? thread.interrupt(); ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } }
輸出:
Thread was interrupted.
被喚醒了處理Exception后可以自由選擇做什么事
Thread completed.
1、被阻塞的線程處于阻塞狀態(tài),比如調(diào)用了 Thread.sleep() 或 Object.wait() 方法。
2、其他線程調(diào)用了被阻塞線程的 interrupt() 方法。
3、被阻塞線程會被喚醒,它會檢查自己是否被中斷,如果被中斷,就會拋出 InterruptedException 異常。
4、此時,被阻塞線程可以選擇如何處理這個異常,比如捕獲異常并做相應(yīng)的處理,或者繼續(xù)向上層拋出異常。
注意:
中斷是一種協(xié)作機制,它并不能直接終止一個線程的執(zhí)行。被中斷的線程需要在適當?shù)臅r候檢查自己是否被中斷,并做出相應(yīng)的響應(yīng)。在處理 InterruptedException 時,可以選擇終止線程的執(zhí)行或采取其他合適的措施來處理中斷。(存在不能被中斷的阻塞 I/O 調(diào)用, 應(yīng)該考慮選擇可中斷的調(diào) =用)。
interrupted() 方法(靜態(tài)方法)
public static boolean interrupted()
interrupted() 方法是一個靜態(tài)方法,用于檢測當前線程是否被中斷,并且會清除中斷狀態(tài)。當一個線程被中斷時,該線程的中斷狀態(tài)會被設(shè)置為 true。當你調(diào)用 interrupted() 方法時,它會返回當前線程的中斷狀態(tài),并且同時將中斷狀態(tài)重置為 false。這意味著,如果連續(xù)多次調(diào)用 interrupted() 方法,只有第一次會返回 true,之后的調(diào)用將返回 false,除非線程又被重新中斷。
public class InterruptedSample { ? ? public static void main(String[] args) { ? ? ? ? Thread thread = new Thread(() -> { ? ? ? ? ? ? for (int i = 0; i < 5; i++) { ? ? ? ? ? ? ? ? if (Thread.interrupted()) { ? ? ? ? ? ? ? ? ? ? System.out.println("Thread is interrupted."); ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? System.out.println("Thread is not interrupted."); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? thread.start(); ? ? ? ? // 主線程休眠一段時間后,中斷被阻塞的線程 ? ? ? ? try { ? ? ? ? ? ? Thread.sleep(2000); ? ? ? ? ? ? thread.interrupt(); ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } }
isInterrupted() 方法(實例方法)
public boolean isInterrupted()
isInterrupted() 方法是一個實例方法,用于檢查當前線程的中斷狀態(tài),但不會清除中斷狀態(tài)。當你調(diào)用 isInterrupted() 方法時,它會返回當前線程的中斷狀態(tài),并且不會改變中斷狀態(tài)。因此,多次調(diào)用 isInterrupted() 方法會一直返回相同的中斷狀態(tài),不會重置為 false。
public class IsInterruptedSample { ? ? public static void main(String[] args) { ? ? ? ? Thread thread = new Thread(() -> { ? ? ? ? ? ? for (int i = 0; i < 5; i++) { ? ? ? ? ? ? ? ? if (Thread.currentThread().isInterrupted()) { ? ? ? ? ? ? ? ? ? ? System.out.println("Thread is interrupted."); ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? System.out.println("Thread is not interrupted."); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? thread.start(); ? ? ? ? // 主線程休眠一段時間后,中斷被阻塞的線程 ? ? ? ? try { ? ? ? ? ? ? Thread.sleep(2000); ? ? ? ? ? ? thread.interrupt(); ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } }
到此這篇關(guān)于java InterruptedException 異常中斷的實現(xiàn)的文章就介紹到這了,更多相關(guān)java InterruptedException 異常中斷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringAnimation 實現(xiàn)菜單從頂部彈出從底部消失動畫效果
最近做項目遇到這樣一個需求,要求實現(xiàn)一種菜單,菜單從頂部彈入,然后從底部消失,頂部彈入時,有一個上下抖動的過程,底部消失時,先向上滑動,然后再向下滑動消失。下面給大家?guī)砹藢崿F(xiàn)代碼,感興趣的朋友一起看看吧2018-05-05使用spring.profiles.active來分區(qū)配置的方法示例
這篇文章主要介紹了使用spring.profiles.active來分區(qū)配置的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01