[问题解决]Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here.


背景以及问题

记处理一次sonarqube扫出来的代码问题。

代码

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.为何是问题? 

这里希望

  1.  Thread.currentThread().interrupt(); 重新标记一下线程中断状态
  2.  或者 再次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();

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM