Acquiring multiple locks atomically in java
I have the following code: Note: I simplified the code as much as possible for readability. If I forgot any critical pieces let me know. public class User(){ private Relations relations; public User(){ relations = new Relations(this); } public getRelations(){ return relations; } } public class Relations(){ private User user; public Relations(User user){ this.user = user; } public synchronized void setRelation(User user2){ Relations relations2 = user2.getRelations(); synchronized(relations2){ storeRelation(user2); if(!relations2.hasRelation(user)) relations2.setRelation(user); } } public