InterruptException处理方法

1.背景:

项目中tryLock()方法带参数时,例如:rLock.tryLock(0, 10, TimeUnit.SECONDS)会抛出InterruptException,

那么,遇到InterruptException怎么处理呢?

为了很好的让大家理解,先假设个场景,玩电脑游戏,你先开机,才能玩游戏。你不可能没开机就开始玩游戏。

那么步骤(1).开机(2).玩游戏

2.处理方法:

项目中推荐第三种方式(在抛出InterruptedException后执行特定步骤)

(1).生吞异常,即不对InterruptException作任何处理(不推荐)

public class TaskRunner implements Runnable {

    @Override
    public 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),并抛出InterruptedException
        thread.interrupt();
    }
}

那么打印结果为:开机失败还能玩游戏,简直是做梦,很显然不符合逻辑

2s后中断线程
开机中。。。。。。
开机失败
java.lang.InterruptedException: sleep interrupted
	at 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 {

    @Override
    public 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),并抛出InterruptedException
        thread.interrupt();
    }
}

打印结果: 是符合我们的逻辑的

2s后中断线程
开机中。。。。。。
开机失败
java.lang.InterruptedException: sleep interrupted
	at 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 {

    @Override
    public 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),并抛出InterruptedException
        thread.interrupt();
    }
}

代码结果: 开机失败没有调用玩游戏接口,逻辑正确

开机中。。。。。。
2s后中断线程
开机失败
java.lang.InterruptedException: sleep interrupted
	at 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 {

    @Override
    public 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();
            //这句话输出true
            System.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),并抛出InterruptedException
        thread.interrupt();
    }
}

打印结果:开机失败没玩游戏,逻辑正常。

开机中。。。。。。
2s后中断线程
开机失败
java.lang.InterruptedException: sleep interrupted
线程是否处于中断状态:false
	at java.lang.Thread.sleep(Native Method)
线程是否处于中断状态:true
	at 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 flag
                b.interrupt(this);
                return;
            }
        }
        interrupt0();
    }

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值