在多核的时代,我们开始编写并发程序,于是碰到了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()吧。