Synchronizing on an object and changing the reference
问题 Let's say I have an object as follows: Map<String, String> m = new HashMap<>(); Then I synchronize on this object as follows and change its reference: synchronize(m){ m = new HashMap<>(); } With this code, what happens to the lock on m? Is it still safe to update the new object represented by m? Or is the lock essentially on the old object? 回答1: From JLS 17.1: The synchronized statement (§14.19) computes a reference to an object; it then attempts to perform a lock action on that object's