Search code examples
javamultithreadinglockingsynchronizedfinal

When a lock holds a non-final object, can the object's reference still be changed by another thread?


When an object needs to be synchronized, the IDE complains if it's not set non-final (because its reference isn't persistent):

private static Object myTable;
....

synchronized(myTable){          //IDE complains!
     //access myTable here...
}

We all know the IDE complains to prevent another thread from entering the guarded block if the thread holding the lock changes the non-final object's references.

But could a synchronized object's reference also be changed by another thread B while thread A holds the lock for the same object?


Solution

  • But could a synchronized object's reference also be changed by another thread B while thread A holds the lock for the same object?

    If you mean "could another thread change the value of the myTable variable, the answer is "absolutely"... assuming there's a code path that would allow that. It's a private variable, so you should be able to find all the code that can change the value.

    Holding a lock just stops another thread from acquiring the same lock. It doesn't have any effect on what code can access which variables, in itself.

    As a side-note, it's important to differentiate between an object, a variable, and the value of the variable (which is a reference, not an object). So there's no such thing as a "final object" - only variables (and classes and methods) can be final. Likewise there's no such thing as a "synchronized object", and you can't change an "object's reference" - you can change the value of a variable so that it's a reference to a different object. Making these distinctions clear in your mind may help you when thinking about what's going on here.