用sequence来避免出现thread dead lock

In other situations, like Listing 3's bank account example, applying the fixed-order rule grows even more complicated; you need to define a total ordering on the set of objects eligible for locking and use this ordering to choose the sequence of lock acquisition. This sounds messy, but is in fact straightforward. Listing 4 illustrates that technique; it uses a numeric account number to induce an ordering on Account objects. (If the object you need to lock lacks a natural identity property like an account number, you can use the Object.identityHashCode() method to generate one instead.


在javaworld上看到的一篇文章,url:http://www.javaworld.com/javaworld/jw-10-2001/jw-1012-deadlock.html?page=3 想起了当初的面试的时候,面试官问我如何避免死锁,当初一股脑的就说用请求超时来避免死锁,但是现在想想,要是真遇到极限的情况,死锁仍然无法避免,所以还是用sequence来避免死锁比较合理,制定一个rule,让每次多个加锁请求都能保证符合指定的rule,按照rule定制的sequence来请求锁,从而避免死锁问题。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值