Singleton Pattern thread Safety

  • Thread starter Thread starter RaghuveerKasyap
  • Start date Start date
R

RaghuveerKasyap

Guest
Hi,

For implementing thread safety in Singleton Pattern, instead of locking can we have the below approach.

1. Creating a private static Boolean Created field with default assigned to "False"

2. Create a private field of class type, Singleton to hold the Singleton object reference.

2. In the constructor, assign the value of Created to "True".

3. Create a method, as

public static Singleton GetInstance()
{
if(!Created) objRef = new Singleton();

return objRef;

}


I am a bit confused, how this approach is different from using lock. I could observe similar behavior while getting this method invoked in multi-threaded environment.


Regards,
Raghuveer

Continue reading...
 
Back
Top