gmemon gmemon - 20 days ago 7
Java Question

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?

Answer

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 monitor and does not proceed further until the lock action has successfully completed. After the lock action has been performed, the body of the synchronized statement is executed. If execution of the body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor.

Now the questions.

What happens to the lock on m?

Nothing. This is a little bit confusing. Actually, the thread is holding the lock on the object referenced by m at the time it was trying to acquire the lock. The assignment to m in the synchronized block does not automatically "switch" the lock that is being held by the executing thread.

Is it still safe to update the new object represented by m?

It's not safe. The write to m is not synchronized on the same lock.

Or is the lock essentially on the old object?

Yes