怎么正确处理InterruptedException

本文探讨了在Java多线程编程中如何正确处理InterruptedException。特别是在线程被中断后,如何确保后续操作能够感知到这一状态,避免不必要的等待。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在多核的时代,我们开始编写并发程序,于是碰到了InterruptedException,SUN的java文档告诉我们,当一个线程在sleep(),wait()或者join()的时候,如果被其他线程中断(通过thread.interrupt()方法),线程内部都会抛出这个InterruptedException异常。


在知道了来龙去脉后,我们开始处理InterruptedException异常,于是,我们有了下面的代码:

 

try
{
     Thread.sleep(1000);
}catch(InterruptedException ex)
{
      logger.error("thread interrupted",ex);
      throw ex;//re-throw 
}

 

上面的代码看起来很正确,没有吃掉异常,有记录异常信息并重新抛出了该异常。但是,对于InterruptedException来说,却有着细小的差别。假设我们有如下代码:

 

public class TestThread extends Thread
{

    @Override
    public void run()
    {
        try
        {
            doSth();
        }
        catch (Exception ex)
        {
            // log exception info
        }

        try
        {
            doSthElse();
        }
        catch (Exception e)
        {
            // log exception info
        }
    }

    public synchronized void doSthElse() throws Exception
    {
        // simulate doing sth else here

        // and then wait
        wait();
    }

    public void doSth() throws Exception
    {
        try
        {
            // simulate doing sth here

            // and sleep a while
            Thread.sleep(1000);
        }
        catch (InterruptedException ex)
        {
            // log exception info
            throw ex;
        }
    }
}

 

 

如果,我们在doSth()的时候,线程被中断了,那么,在doSth()方法里进行Thread.sleep(1000)的时候,InterruptedException会被抛出来,但是,doSthElse()依然被执行,等待被其他线程唤醒,可是,该线程已经被中断了。

 

所以,在处理InterruptedException的时候,不要吝啬,再加上一句Thread.currentThread().interrupt()吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值