java InterruptedException 異常中斷的實(shí)現(xiàn)
InterruptedException
當(dāng)一個(gè)線程在被阻塞狀態(tài)(如調(diào)用 Thread.sleep() 或 Object.wait() 方法)時(shí),如果其他線程調(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) { ? ? ? ? ? ? // 被中斷時(shí)會拋出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(); ? ? ? ? // 主線程休眠一段時(shí)間后,中斷被阻塞的線程 ? ? ? ? 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、此時(shí),被阻塞線程可以選擇如何處理這個(gè)異常,比如捕獲異常并做相應(yīng)的處理,或者繼續(xù)向上層拋出異常。
注意:
中斷是一種協(xié)作機(jī)制,它并不能直接終止一個(gè)線程的執(zhí)行。被中斷的線程需要在適當(dāng)?shù)臅r(shí)候檢查自己是否被中斷,并做出相應(yīng)的響應(yīng)。在處理 InterruptedException 時(shí),可以選擇終止線程的執(zhí)行或采取其他合適的措施來處理中斷。(存在不能被中斷的阻塞 I/O 調(diào)用, 應(yīng)該考慮選擇可中斷的調(diào) =用)。
interrupted() 方法(靜態(tài)方法)
public static boolean interrupted()
interrupted() 方法是一個(gè)靜態(tài)方法,用于檢測當(dāng)前線程是否被中斷,并且會清除中斷狀態(tài)。當(dāng)一個(gè)線程被中斷時(shí),該線程的中斷狀態(tài)會被設(shè)置為 true。當(dāng)你調(diào)用 interrupted() 方法時(shí),它會返回當(dāng)前線程的中斷狀態(tài),并且同時(shí)將中斷狀態(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(); ? ? ? ? // 主線程休眠一段時(shí)間后,中斷被阻塞的線程 ? ? ? ? try { ? ? ? ? ? ? Thread.sleep(2000); ? ? ? ? ? ? thread.interrupt(); ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } }
isInterrupted() 方法(實(shí)例方法)
public boolean isInterrupted()
isInterrupted() 方法是一個(gè)實(shí)例方法,用于檢查當(dāng)前線程的中斷狀態(tài),但不會清除中斷狀態(tài)。當(dāng)你調(diào)用 isInterrupted() 方法時(shí),它會返回當(dāng)前線程的中斷狀態(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(); ? ? ? ? // 主線程休眠一段時(shí)間后,中斷被阻塞的線程 ? ? ? ? try { ? ? ? ? ? ? Thread.sleep(2000); ? ? ? ? ? ? thread.interrupt(); ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } }
到此這篇關(guān)于java InterruptedException 異常中斷的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java InterruptedException 異常中斷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 如何實(shí)現(xiàn)正確的刪除集合中的元素
這篇文章主要介紹了java 如何實(shí)現(xiàn)正確的刪除集合中的元素,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Java設(shè)計(jì)模式之動(dòng)態(tài)代理
今天小編就為大家分享一篇關(guān)于Java設(shè)計(jì)模式之動(dòng)態(tài)代理,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01SpringAnimation 實(shí)現(xiàn)菜單從頂部彈出從底部消失動(dòng)畫效果
最近做項(xiàng)目遇到這樣一個(gè)需求,要求實(shí)現(xiàn)一種菜單,菜單從頂部彈入,然后從底部消失,頂部彈入時(shí),有一個(gè)上下抖動(dòng)的過程,底部消失時(shí),先向上滑動(dòng),然后再向下滑動(dòng)消失。下面給大家?guī)砹藢?shí)現(xiàn)代碼,感興趣的朋友一起看看吧2018-05-05Java實(shí)現(xiàn)布隆過濾器的幾種方式總結(jié)
這篇文章給大家總結(jié)了幾種Java實(shí)現(xiàn)布隆過濾器的方式,手動(dòng)硬編碼實(shí)現(xiàn),引入Guava實(shí)現(xiàn),引入hutool實(shí)現(xiàn),通過redis實(shí)現(xiàn)等幾種方式,文中有詳細(xì)的代碼和圖解,需要的朋友可以參考下2023-07-07使用spring.profiles.active來分區(qū)配置的方法示例
這篇文章主要介紹了使用spring.profiles.active來分區(qū)配置的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01