Reprint Please specify source:http://blog.csdn.net/ns_code/article/details/17200937
Most of the code is not prone to deadlocks, and deadlocks can be hidden in code for quite a long time, waiting for uncommon conditions to occur, but even a small probability can cause devastating damage if it happens. Avoiding deadlocks is a difficult thing to do, follow these guidelines to help avoid deadlocks:
1, only in the shortest time necessary to hold the lock, consider the use of synchronous statement block instead of the entire synchronization method;
2. Try to write code that does not need to hold multiple locks at the same time, and if unavoidable, make sure the thread holds the second lock for as short a time as possible;
3, create and use a large lock to replace a number of small locks, and the lock for mutual exclusion, rather than as an object-level lock for a single object;
Java concurrent Programming (ix) deadlock