Following is a specific case of a deadlock:
There are 2 threads-- T0 & T1, and 2 resources-- R0 & R1.
T0 is holding the lock of R0 & waiting to access R1 to release R0. T1 is doing the same the other way round-- holding R1 & waiting for R0.
A way to resolve this that I can think of:
After a certain time of wait-- which supposedly indicates this infinite loop, Tx, x=0 or 1, writes the state of Rx to an object Ox and sets a flag Fx that it is done with updating Rx. It also checks the flag F(1-x) to see whether it is set-- if so, reads the state from O(1-x), proceeds accordingly & releases Rx. In this scenario, T0 & T1 are prioritized (Thread class of Java has means to prioritize threads(?), or this can be achieved by a class instance-- one of the threads created with a specific value on a static variable is taking charge in breaking this loop.)
This works if T(1-x)'s further execution on R(1-x) upon acquiring Rx doesn't alter the state of R(1-x).
What is wrong with this scenario?
Is there another/better way to handle this?
I'm aware of managing the access to resources in an ordered fashion as pointed out in many articles.