Essentially, you have the problem of dining philosophy.
https://en.wikipedia.org/wiki/Dining_philosophers_problem
Ovidiu Lupas answer is similar to Dijkstra Resource Heirarchy solution, but there are 3 more solutions explained on the wiki page
This is what the arbiter's decision looks like. If all the objects that you use are inherited from the same type, you can use static class variables to implement arbiters in the object class.
import java.util.concurrent.locks.Lock; public void init() { Lock arbitrator = new Lock(); } public void meth1() { arbitrator.lock(); synchronized (obj1) { synchronized (obj2) { arbitrator.unlock();
The Chandy / Misra solution requires a lot of posts, so I'm not going to implement it, but wikipedia has a pretty good explanation.
Syncryptic Jan 15 '15 at 20:18 2015-01-15 20:18
source share