Correctly synchronizing equals() in Java
问题 I have the following class which contains only one field i . Access to this field is guarded by the lock of the object ("this"). When implementing equals() I need to lock this instance (a) and the other (b). If thread 1 calls a.equals(b) and at the same time thread 2 calls b.equals(a), the locking order is reverse in the two implementations and may result in deadlock. How should I implement equals() for a class which has synchronized fields? public class Sync { // @GuardedBy("this") private