代码
public void main() {
try {
Thread.sleep(100L);
} catch (Exception e) {
log.error("err. "e);
}
}
sonar提示问题
Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here.为何是问题?
这里希望
- Thread.currentThread().interrupt(); 重新标记一下线程中断状态
- 或者 再次throw InterruptedException
所以 对应1的正确写法:
public void main() {
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
log.error("err", e);
Thread.currentThread().interrupt();
} catch (Exception e) {
log.error("err. ",e);
}
}
原因
java interrup()函数会中断线程(本质更新线程为中断状态)。若sleep()函数检测到线程中断(interrupt()函数触发) 会抛 InterruptedException, 被catch住后线程中断状态更新为未中断(参考:https://www.codenong.com/cs106837995/)。
sonar检测到该问题会提示此种潜在逻辑,防止忽略该潜在风险而导致代码bug。
所以,正确处理方式需要手动再次触发一下Thread.currentThread().interrupt();