I have two threads Thread1 and Thread2
//Within Thread1
synchronized(obj1)
{
obj1 = null;
}
//Within Thread2
synchronized(obj1)
{
do something
}
If jvm first executes thread1 and sets obj1 to null, then will thread2 see that change immediately or will it take time and jvm could still run the thread2 synchronized block since obj1 is not yet null?
This will almost certainly break the synchronization abstraction — I wouldn’t be confident that
thread2will see the change immediately. You should never change the reference of the object you’re synchronizing on, much less set it tonull, which will cause aNullPointerExceptionon any further attempts to synchronize on it.