Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

synchronized object set to null

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?

like image 467
vjk Avatar asked Apr 17 '12 16:04

vjk


1 Answers

This will almost certainly break the synchronization abstraction -- I wouldn't be confident that thread2 will see the change immediately. You should never change the reference of the object you're synchronizing on, much less set it to null, which will cause a NullPointerException on any further attempts to synchronize on it.

like image 79
Louis Wasserman Avatar answered Sep 30 '22 22:09

Louis Wasserman