-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[建议]lock.lock()应该放在try之前 #287
Comments
@yangguanbao 看下哈 |
@juggernaut0425 @yangguanbao |
已经采纳了你的建议,非常感谢,我们已经在内部版本中加了此规约描述,云栖大会版本中会出现这个条目,十分感谢你对《阿里巴巴JAVA开发手册》的支持,内容如下: 【强制】在try代码块之前调用Lock实现类的lock()方法,避免由于加锁失败,导致finally调用unlock()抛出异常。 |
但是如果lock.lock()放在外面,就有可能出现锁了之后最终没有lock.unlock()的情况,这样会造成死锁。 |
也就是说,大伙认为抛出异常的风险、和造成死锁的风险,后者更容易被接受?请指教 |
对于:
基于以上条件, 似乎不会出现所描述的这个死锁问题, 如有不对也请指教 |
建议新增规则,lock.lock()应该放在try之前:
如果按照下边写法,当lock.lock()报错时也会进入finally释放锁,根据Lock.unlock()文档,当非锁持有线程调用该方法时会抛出unchecked异常:
The text was updated successfully, but these errors were encountered: