Whenever you have multiple locks A, B and C you can have deadlock if you don't guarantee that your code attempts to acquire said locks in the same order.
final Lock A = new ReentrantLock();
final Lock B = new ReentrantLock();
final Lock C = new ReentrantLock();
A,B,C or C,B,A, or A,C,B - it does not matter so long as the order is consistent.
A problem arises when you have one code path try for: A,B,C And another trying for C,B,A.
As you can probably guess since A and C are both being held, one of the two will get B and then both will deadlock. (Aka you have a cycle in the resource locking graph)
Formally speaking deadlock can arise only if all of the following conditions hold:
- No Preemption: The system will not free resources after allocation; they can only be released by the holding process.
- Circular Wait: Discussed above.
- Mutual Exclusion: Only one process can use a resource at any given time.
- Resource Holding: A process is currently holding at least one resource and requesting/waiting for additional resources which are held by another process.
The best solution is to make sure the order is consistent or lock at a higher (single) level. Another option is to use a locking library that will timeout while attempting to lock (or use conditions and write your own wrapper that does this). But that approach is not for the faint of heart. Some implementation of this will wait a random amount of time and try again, but this can be highly inefficient as the number of locks increases.
Resources:
- Here is a practical article on analyzing deadlock in Java that you might be interested in: http://www.journaldev.com/1058/java-deadlock-example-and-how-to-analyze-deadlock-situation
- You can also use open source tools like JCarder to find deadlock: http://www.jcarder.org/ which for programs with large dumps can be easier then trying to grok dump files.
P.S. I didn't actually read much of your code since its poorly formatted and and is not a minimal example (ie. too verbose for our purposes here). But this advice should answer you question from a theoretical standpoint.