2013-01-11 21 views
0

假设我有一个线程类是这样的:中断的线程是否会继续其事务?

public class ThreadClass extends Thread{ 
     Object object = new Object(); //relevant object 

     public void run(){ 
      synchronized(object){ 
       if(/*condition is true*/){ 
        //do transactions here 
       }else{ 
        try{ 
         object.wait(); 
        }catch(InterruptedException e){ 
         //if thread was interrupted 
        } 
       } 

       //other transactions here 
      } 
     } 

} 

如果当前线程被中断,它会继续交易?是否还会去其他交易?谢谢。

+1

通过一个调试器运行代码......当你逐步浏览它时,你会注意到它继续其事务,因为'InterruptedException'被捕获并被有效地“吞噬”,但是如果在此之后发出另一个中断在其他交易中,它会杀死线程。 – mre

回答

1

如果当前线程被中断,它会继续其交易吗?

是的,但中断标志将被设置(调用Thread.interrupted()将返回true),但这不会影响正在执行的代码。

它还会继续在此处进行其他交易吗?

是的,基于与上述相同的原因。如果执行object.wait(),则会执行其他事务,然后InterruptedException被捕获,假设您没有返回catch块。