java InterruptedException 異常中斷的實現(xiàn)
InterruptedException
當(dāng)一個線程在被阻塞狀態(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í)行。被中斷的線程需要在適當(dāng)?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)方法,用于檢測當(dāng)前線程是否被中斷,并且會清除中斷狀態(tài)。當(dāng)一個線程被中斷時,該線程的中斷狀態(tài)會被設(shè)置為 true。當(dāng)你調(diào)用 interrupted() 方法時,它會返回當(dāng)前線程的中斷狀態(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() 方法是一個實例方法,用于檢查當(dāng)前線程的中斷狀態(tài),但不會清除中斷狀態(tài)。當(dāng)你調(diào)用 isInterrupted() 方法時,它會返回當(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();
? ? ? ? // 主線程休眠一段時間后,中斷被阻塞的線程
? ? ? ? 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)文章
Java中的字節(jié),字符輸出流與字節(jié)和字符輸入流的簡單理解
這篇文章主要介紹了java 字節(jié)流和字符流的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-07-07
Mybatis-plus查詢語句加括號(.or(),.and())問題
這篇文章主要介紹了Mybatis-plus查詢語句加括號(.or(),.and())問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
java后端+前端使用WebSocket實現(xiàn)消息推送的詳細(xì)流程
后端向前端推送消息就需要長連接,首先想到的就是websocket,下面這篇文章主要給大家介紹了關(guān)于java后端+前端使用WebSocket實現(xiàn)消息推送的詳細(xì)流程,需要的朋友可以參考下2022-10-10

