posted 10 years ago
To avoid deadlock, one of the rules-of-thumb is:
If a thread, during its flow, is acquiring locks L1 and L2 in a specific order, then all the threads should acquire the locks in the same order.
Currently, in OP's code, first thread locks r1 and then attempts to lock r2. However second thread does exactly opposite - this is classic scenario of deadlock.
Of course, suggestions by Ulf and Winston would work, and the rule mentioned above is not silver-bullet solution to fix deadlock issues, but following that rule has saved a lot of my time(and I could identify potential deadlocks before they actually occurred).
I hope this helps.
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)