Java Reference
In-Depth Information
A program will be free of lock-ordering deadlocks if all threads acquire the locks they need
in a fixed global order.
Verifying consistent lock ordering requires a global analysis of your program's locking be-
havior. It is not sufficient to inspect code paths that acquire multiple locks individually; both
leftRight and rightLeft are “reasonable” ways to acquire the two locks, they are just
not compatible. When it comes to locking, the left hand needs to know what the right hand is
doing.
Listing 10.1. Simple Lock-ordering Deadlock. Don't do this.
10.1.2. Dynamic Lock Order Deadlocks
Sometimes it is not obvious that you have sufficient control over lock ordering to prevent
deadlocks. Consider the harmless-looking code in Listing 10.2 that transfers funds from one
account to another. It acquires the locks on both Account objects before executing the trans-
Search WWH ::




Custom Search