• Post Reply Bookmark Topic Watch Topic
  • New Topic

singleton synchronized doubt

 
vanlalhmangaiha khiangte
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a singleton class which has a method as below




As you can seen from the code there are two check
if( instance == null )
The first check i understood.
The second one is the one that i am having a doubt..

Multiple threads can access this class and


This code above ensures that if the same thread access this class again,then it will return the same object else it will create a new one.

If i am wrong please tell me why there are two checks for
if( instance == null )

And is this check necessary for multiple threads ..

Thanks in advance,
 
arulk pillai
Author
Ranch Hand
Posts: 3402
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need the second check. Let us take a scenario where there are two threads concurrently accessing your getInstance method


Thread 1: Gets the lock on SingleTon.class and creating an instance.

Thread 2: Has passed the outer if(instance == null) and waiting for the lock. Once the lock is released by the first thread it goes inside. If we do not re-check if(instance == null) we may create a new instance. The thread 1 has already created an instance.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the getInstance method is using the "double checked locking" principle.

The idea is (in pseudocode):


The way it is used in the getInstance method looks like it is for performance. There is no point in using the resources to get a lock on the Singleton.class if the singleton instance has already been created.

By the way, apparently double checked locking doesn't always work in java. I'm no expert on this but see here for more info
 
arulk pillai
Author
Ranch Hand
Posts: 3402
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might as well synchronize on the method level. you are not doing any intensive operations in your getInstance method.
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although I agree it is easier, there is most definitely a (small) performance hit when synchronizing the entire method.

If instance != null, you don't need to enter the synchronized block at all. If the entire method is synchronized you will synchronize unnecessarily. Since synchronization has some overhead synchronzing the entire method will be slower.
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to make instance static volatile.

Also this is discussed by Bloch in Effective Java.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as far as I'm aware, marking it as volatile can be done in earlier versions of java but only works correctly for double checked locking in 1.5 and later.
 
vipin raimcs
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's true that there would be performance hit if we used synchronization over entire method so its better to use synchronize block and do the checks which have done before for safety.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!