InterruptException处理方法
1.背景:
项目中tryLock()方法带参数时,例如:rLock.tryLock(0, 10, TimeUnit.SECONDS)会抛出InterruptException,
那么,遇到InterruptException怎么处理呢?
为了很好的让大家理解,先假设个场景,玩电脑游戏,你先开机,才能玩游戏。你不可能没开机就开始玩游戏。
那么步骤(1).开机(2).玩游戏
2.处理方法:
项目中推荐第三种方式(在抛出InterruptedException后执行特定步骤)
(1).生吞异常,即不对InterruptException作任何处理(不推荐)
public class TaskRunner implements Runnable {@Overridepublic void run() {//第一步 开机startComputer();//第二步 玩游戏playGame();}private void startComputer() {try {System.out.println("开机中。。。。。。");Thread.sleep(3000);System.out.println("开机成功");} catch (InterruptedException e) {e.printStackTrace();System.out.println("开机失败");}}private void playGame() {System.out.println("游戏ing");}
}class Test {public static void main(String[] args) {TaskRunner taskRunner = new TaskRunner();Thread thread = new Thread(taskRunner);thread.start();try {System.out.println("2s后中断线程");TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}//中断线程,执行完这个后,线程在wait,join,sleep中被阻塞,清除它的中断状态(isInterrupted() is false),并抛出InterruptedExceptionthread.interrupt();}
}
那么打印结果为:开机失败还能玩游戏,简直是做梦,很显然不符合逻辑
2s后中断线程
开机中。。。。。。
开机失败
java.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at com.test.TaskRunner.startComputer(TaskRunner.java:30)at com.test.TaskRunner.run(TaskRunner.java:16)at java.lang.Thread.run(Thread.java:745)
游戏ing
(2).向上层代码抛出异常,即在开机失败后继续抛InterruptException,让我们知道,玩不了游戏了,就不调用玩游戏方法了
public class TaskRunner implements Runnable {@Overridepublic void run() {//第一步try {startComputer();} catch (InterruptedException e) {//开机被中断,我玩不了游戏了,退出return;}//第二步playGame();}private void startComputer() throws InterruptedException {try {System.out.println("开机中。。。。。。");Thread.sleep(3000);System.out.println("开机成功");} catch (InterruptedException e) {e.printStackTrace();System.out.println("开机失败");throw e;}}private void playGame() {System.out.println("游戏ing");}
}class Test {public static void main(String[] args) {TaskRunner taskRunner = new TaskRunner();Thread thread = new Thread(taskRunner);thread.start();try {System.out.println("2s后中断线程");TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}//执行完这个后,线程在wait,join,sleep中被阻塞,清除它的中断状态(isInterrupted() is false),并抛出InterruptedExceptionthread.interrupt();}
}
打印结果: 是符合我们的逻辑的
2s后中断线程
开机中。。。。。。
开机失败
java.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at com.test.TaskRunner.startComputer(TaskRunner.java:30)at com.test.TaskRunner.run(TaskRunner.java:16)at java.lang.Thread.run(Thread.java:745)
(3).在抛出InterruptedException后执行特定步骤(推荐)
在这里,我们可以在抛出InterruptedException后,把玩游戏的返回值设为false,让主程序知道,就不调用玩游戏的步骤了
public class TaskRunner implements Runnable {@Overridepublic void run() {//第一步boolean b = startComputer();//第二步if (b) {playGame();}}private boolean startComputer() {try {System.out.println("开机中。。。。。。");Thread.sleep(3000);System.out.println("开机成功");return true;} catch (InterruptedException e) {e.printStackTrace();System.out.println("开机失败");return false;}}private void playGame() {System.out.println("游戏ing");}
}class Test {public static void main(String[] args) {TaskRunner taskRunner = new TaskRunner();Thread thread = new Thread(taskRunner);thread.start();try {System.out.println("2s后中断线程");TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}//执行完这个后,线程在wait,join,sleep中被阻塞,清除它的中断状态(isInterrupted() is false),并抛出InterruptedExceptionthread.interrupt();}
}
代码结果: 开机失败没有调用玩游戏接口,逻辑正确
开机中。。。。。。
2s后中断线程
开机失败
java.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at com.test.TaskRunner.startComputer(TaskRunner.java:25)at com.test.TaskRunner.run(TaskRunner.java:15)at java.lang.Thread.run(Thread.java:745)Process finished with exit code 0
(4).恢复中断状态,让调用堆栈上的较高代码知道并处理
注释有对Thread.currentThread().interrupt()的介绍。
public class TaskRunner implements Runnable {@Overridepublic void run() {//第一步startComputer();//第二步if (!Thread.currentThread().isInterrupted()) {playGame();}}private void startComputer() {try {System.out.println("开机中。。。。。。");Thread.sleep(3000);System.out.println("开机成功");} catch (InterruptedException e) {e.printStackTrace();System.out.println("开机失败");//这句话输出false,因为线程在wait,join,sleep中被阻塞会清除它的中断状态,所以在抛出InterruptedException后,认为你已经知道程序被中断了,清除了中断状态System.out.println("线程是否处于中断状态:" + Thread.currentThread().isInterrupted());//阻塞方法在抛出InterruptedException时会清除当前线程的中断状态,如果此时不恢复中断状态,也不抛出InterruptedException,那中断信息便会丢失,上层调用者也无法得知中断的发生。//让上层代码知道线程被打断Thread.currentThread().interrupt();//这句话输出trueSystem.out.println("线程是否处于中断状态:" + Thread.currentThread().isInterrupted());}}private void playGame() {System.out.println("游戏ing");}
}class Test {public static void main(String[] args) {TaskRunner taskRunner = new TaskRunner();Thread thread = new Thread(taskRunner);thread.start();try {System.out.println("2s后中断线程");TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}//执行完这个后,线程在wait,join,sleep中被阻塞,清除它的中断状态(isInterrupted() is false),并抛出InterruptedExceptionthread.interrupt();}
}
打印结果:开机失败没玩游戏,逻辑正常。
开机中。。。。。。
2s后中断线程
开机失败
java.lang.InterruptedException: sleep interrupted
线程是否处于中断状态:falseat java.lang.Thread.sleep(Native Method)
线程是否处于中断状态:trueat com.test.TaskRunner.startComputer(TaskRunner.java:25)at com.test.TaskRunner.run(TaskRunner.java:15)at java.lang.Thread.run(Thread.java:745)
根据特定的情景去用不同的处理方法
3.项目中经常见到的:
比如:
service里面增加或者修改会加锁,可能抛InterruptExcetion
然后怎么操作
1.抛出去InterruptExcetion给了上层controller,controller对这个进行处理
2.上层controller不需要知道service遇到什么异常,service直接返回结果失败,controller再进行处理
我认为第二种处理方法会比较好,controller不需要知道那么多细节
4.interrupt()分析
/*** 中断线程** 除非当前线程中断自身,* 否则在checkAccess中将会抛出SecurityException** 如果当前线程在 wait、join、sleep 中被阻塞,* 将会清除它的中断状态(isInterrupted() is false),* 并抛出InterruptedException** 如果当前线程在java.nio.channels上的I/O操作被阻塞,* 则通道将被关闭,线程的中断状态被设置(isInterrupted() is true),* 并抛出ClosedByInterruptException** 如果当前线程在java.nio.channels.Selector中被阻塞,* 则线程的中断状态被设置(isInterrupted() is true),* 并立即返回,可能带有非零值。* 类似于调用了java.nio.channels.Selector的wakeup方法** 如果上面的条件都不存在,* 则线程的中断状态被设置(isInterrupted() is true)**/public void interrupt() {if (this != Thread.currentThread())checkAccess();synchronized (blockerLock) {Interruptible b = blocker;if (b != null) {interrupt0(); // Just to set the interrupt flagb.interrupt(this);return;}}interrupt0();}
InterruptException处理方法相关推荐
- 操作系统(3)-线程的六大状态、基于代码实战的线程创建及六个常用方法
一.进程和线程的简介 进程的概念 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位. 进程中所包含的一个或多个执行单元成为线程.进程还拥有一个私 ...
- 在 Oracle Enterprise Linux 和 iSCSI 上构建您自己的 Oracle RAC 11g 集群
作者:Jeffrey Hunter 了解如何以低于 2,700 美元的费用在 Oracle Enterprise Linux 上安装并配置 Oracle RAC 11g 第 2 版开发集群. 本指南中 ...
- Java面试题大全2021版
一.Java 基础 JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,java 开发工具包,提供了 java 的开发环境和运行环境. JRE:Java Run ...
- interrupt InterruptException
JDK1.6中的interrupt函数: public void interrupt() 中断线程 如果当前线程没有中断它自己(这在任何情况下都是允许的),则该线程的 checkAccess 方法就 ...
- java sleep方法_百战程序员:java线程的休眠和回复
sleep()介绍 sleep() 定义在Thread.java中. sleep() 的作用是让当前线程休眠,即当前线程会从"运行状态"进入到"休眠(阻塞)状态" ...
- java 销毁线程_线程 学习教程(一): Java中终止(销毁)线程的方法
结束线程有以下三种方法: (1)设置退出标志,使线程正常退出,也就是当run()方法完成后线程终止 (2)使用interrupt()方法中断线程 (3)使用stop方法强行终止线程(不推荐使用,Thr ...
- 线程的退出 java_(转)Java结束线程的三种方法
背景:面试过程中问到结束线程的方法和线程池shutdown shutdownnow区别以及底层的实现,当时答的并不好. 线程属于一次性消耗品,在执行完run()方法之后线程便会正常结束了,线程结束后便 ...
- java中断线程_Java中断线程的方法
使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一个标志,表示它已经被中断,并立即返回.这 ...
- Java结束线程的三种方法
转载自https://blog.csdn.net/xu__cg/article/details/52831127 线程属于一次性消耗品,在执行完run()方法之后线程便会正常结束了,线程结束后便会销毁 ...
最新文章
- will_paginate 用作查询分页的注意事项
- 开发板与linux文件系统,基于topeer 4412开发板 ***面linux文件系统的制作
- Linux终端打开一只小马,Linux 终端上的漂亮小马
- orb-slam2在PC和ARM上运行
- 2019我做成的事情
- 网络广告中的CPS,CPA,CPL,CPC,CPM,CPR的含义是什么?
- maven安装Ojdbc6
- 淘宝开源项目TbSchedule的部署和使用
- 浙大PAT甲级1061-1080题目详细代码解答|标准答案|C++语言|浙软机试
- 传奇架设gom引擎常见问题
- 接口测试用例生成工具介绍及应用
- [LeetCode]347. 前 K 个高频元素
- 秦纪三 二世皇帝下二年(癸已、前208)——摘要
- 修复 Apple “连续互通” 方法汇总:修复 AirDrop(隔空投送),修复 Apple Watch 解锁 Mac,修复 HandOff(接力)等
- 大数据小项目之电视收视率企业项目11
- SIM7600CE模块MQTT协议的AT指令流程
- 我不常用的Linux命令
- 大数据系列之日志数据实时分析计算
- 创建FTP服务器下载文件时出现`当前的安全设置不允许从该位置下载文件`
- 多声道、高压、澎湃的影院音效——AC3