: ( , .., , if(!relations.hasRelation(user)) relations2.setRelation(user2); if(!relations2.hasRelation(user)) relations2.setRelation(user);), : x, y, y, x, , .
- Relations. , lockOrder:
private final int lockOrder;
currentLockOrder:
private static int currentLockOrder = 0;
, Relations, lockOrder currentLockOrder :
public Relations()
{
synchronized(Relations.class)
{
lockOrder = currentLockOrder;
++currentLockOrder;
}
}
Relations , lockOrder. setRelation :
public void setRelation(final User thatUser)
{
final Relations that = thatUser.getRelations();
synchronized(lockOrder < that.lockOrder ? this : that)
{
synchronized(lockOrder < that.lockOrder ? that : this)
{
storeRelation(thatUser);
if(! that.hasRelation(user))
that.storeRelation(user);
}
}
}
, x, y, x, y, , .
, setRelation storeRelation. setRelation , ?
, , : x.setRelation(u_y) x.storeRelation(u_y) , y.setRelation(u_x) ( y.storeRelation(u_x)), y ? . , , . ( Relations.storeRelation(...), , .)