p3c 此规则无法满足:锁【lock.lockInterruptibly】必须紧跟try代码块,且unlock要放到finally第一行。

qlckcl4x  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(231)

规约原文

锁【lock.lockInterruptibly】必须紧跟try代码块,且unlock要放到finally第一行。

问题描述

如下代码

Thread t2 = new Thread(() -> { try { lock.lockInterruptibly(); System.out.println("if lock thread is interrupted, it will run"); TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { System.out.println("interrupted"); } finally { lock.unlock(); } });

lock.lockInterruptibly() 如果紧跟try,那么就没办法catch exception了,所以这条规则无法满足

修改建议

相关问题