假设我有以下代码:
现在,
如果我触发了
此外,如果我有:
while(!Thread.currentThread().isInterrupted()){
//do something
Thread.sleep(5000);
}
现在,
Thread.sleep
抛出`InterruptedException`,因此应该像这样:while(!Thread.currentThread().isInterrupted()){
//do something
try{
Thread.sleep(5000);
} catch(InterruptedException e){
}
}
如果我触发了
catch
,那么while
循环会继续吗?还是需要调用Thread.currentThread().interrupt()
?如果我调用这个方法,那么是否也会引起InterruptedException
?否则我怎么会首先遇到异常呢?此外,如果我有:
while (!Thread.currentThread().isInterrupted()){
//do something
callMethod();
}
private void callMethod(){
//do something
try {
Thread.sleep(5000);
} catch(InterruptedException e){
}
}
我的while
循环会再次中断吗?