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

