用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来请求锁,从而避免死锁问题。

你可能感兴趣的:(sequence)