规约原文
锁【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了,所以这条规则无法满足
1条答案
按热度按时间zpqajqem1#
@GreyZeng 好的 我们看看