浅谈Java由于不当的执行顺序导致的死锁
我们来讨论一个经常存在的账户转账的问题。账户A要转账给账户B。为了保证在转账的过程中A和B不被其他的线程意外的操作,我们需要给A和B加锁,然后再进行转账操作, 我们看下转账的代码:
public void transferMoneyDeadLock(Account from,Account to, int amount) throws InsufficientAmountException { synchronized (from){ synchronized (to){ transfer(from,to,amount); } } } private void transfer(Account from,Account to, int amount) throws InsufficientAmountException { if(from.getBalance() < amount){ throw new InsufficientAmountException(); }else{ from.debit(amount); to.credit(amount); } }
看起来上面的程序好像没有问题,因为我们给from和to都加了锁,程序应该可以很完美的按照我们的要求来执行。
那如果我们考虑下面的一个场景:
A:transferMoneyDeadLock(accountA, accountB, 20) B:transferMoneyDeadLock(accountB, accountA, 10)
如果A和B同时执行,则可能会产生A获得了accountA的锁,而B获得了accountB的锁。从而后面的代码无法继续执行,从而导致了死锁。
对于这样的情况,我们有没有什么好办法来处理呢?
加入不管参数怎么传递,我们都先lock accountA再lock accountB是不是就不会出现死锁的问题了呢?
我们看下代码实现:
private void transfer(Account from,Account to, int amount) throws InsufficientAmountException { if(from.getBalance() < amount){ throw new InsufficientAmountException(); }else{ from.debit(amount); to.credit(amount); } } public void transferMoney(Account from,Account to, int amount) throws InsufficientAmountException { int fromHash= System.identityHashCode(from); int toHash = System.identityHashCode(to); if(fromHash < toHash){ synchronized (from){ synchronized (to){ transfer(from,to, amount); } } }else if(fromHash < toHash){ synchronized (to){ synchronized (from){ transfer(from,to, amount); } } }else{ synchronized (lock){ synchronized (from) { synchronized (to) { transfer(from, to, amount); } } } } }
上面的例子中,我们使用了System.identityHashCode来获得两个账号的hash值,通过比较hash值的大小来选定lock的顺序。
如果两个账号的hash值恰好相等的情况下,我们引入了一个新的外部lock,从而保证同一时间只有一个线程能够运行内部的方法,从而保证了任务的执行而不产生死锁。
以上就是浅谈Java由于不当的执行顺序导致的死锁的详细内容,更多关于Java由于不当的执行顺序导致的死锁的资料请关注脚本之家其它相关文章!
相关文章
详解MyBatis-Plus updateById方法更新不了空字符串/null解决方法
这篇文章主要介绍了详解MyBatis-Plus updateById方法更新不了空字符串/null解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-09-09springboot2.3之后hibernate-validator依赖缺失【踩坑】
这篇文章主要介绍了springboot2.3之后hibernate-validator依赖缺失【踩坑】,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-11-11IDEA基于支付宝小程序搭建springboot项目的详细步骤
这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-04-04
最新评论