Singleton pattern is very popular pattern used by programmer. The idea behind the singleton pattern is to have only one instance of a class at any time. This kind of pattern is very useful for for heavy resources like Linq Data Context etc. It is also useful in multithreaded application where different thread of application try to crate instance of one class but it should be thread safe. It can be also useful for global resources and state objects. In singleton class there are two things required. One static variable which hold the instance of class and other is a static method which will return the instance of singleton class. Here is the example of singleton class
public class MySingleTon
{
private static MySingleTon mySingleTonInstance = new MySingleTon();
private MySingleTon()
{
//private constructor
}
public static MySingleTon GetInstace()
{
return mySingleTonInstance;
}
}
MySingleTon objSingleTon = MySingleTon.GetInstace();The above example is not thread safe so it may be possible that different thread can create different instance of classes. To make above singleton class thread safe we need to change code like following.
public class MySingleTon
{
private static MySingleTon mySingleTonInstance = new MySingleTon();
private MySingleTon()
{
//private constructor
}
public static MySingleTon GetInstace()
{
lock (typeof(MySingleTon))
{
if (mySingleTonInstance == null)
{
mySingleTonInstance = new MySingleTon();
}
return mySingleTonInstance;
}
}
}
That thread safe solution is not very elegant. You are needlessly locking always GetInstance() is called, and locking to typeof(MySingleTon) may cause unwanted lost of performace or even deadlocks.
ReplyDeleteCan you please suggest other way of doing this? I don't think that there will be deadlock.
ReplyDeleteLast code must be
ReplyDeletepublic class MySingleTon
private static MySingleTon mySingleTonInstance = new MySingleTon();
private MySingleTon()
{
//private constructor
}
public static MySingleTon GetInstace()
{
if (mySingleTonInstance == null)
{
lock (typeof(MySingleTon))
{
if (mySingleTonInstance == null)
{
mySingleTonInstance = new MySingleTon();
}
return mySingleTonInstance;
}
}
}
}
You can refer to msdn article http://msdn.microsoft.com/en-us/library/ms998558.aspx that explains how to implement.
ReplyDeleteIn their article the lock is only done when there is no current instance created. The instance variable is declared as "volatile"
Other people may lock on typeof(MySingleton) as well, increasing the chances of a deadlock.
ReplyDeleteOn C# (and only on it, AFAIK) you can double check safely and so make something like this:
public static MySingleTon GetInstace()
{
if (mySingleTonInstance == null)
{
lock (lockObj)
{
if (mySingleTonInstance == null)
{
mySingleTonInstance = new MySingleTon();
}
}
}
return mySingleTonInstance;
}
Also, leaving the instance creation to the CLR as in:
private static readonly MySingleTon mySingleTonInstance = new MySingleTon();
This is already thread-safe and good enough in most occasions.
If you want full lazy instantiation this is the way to go:
public sealed class Singleton
{
private Singleton() {}
public static Singleton GetInstance()
{
return NestedSingleton.singleton;
}
class NestedSingleton
{
internal static readonly Singleton singleton = new Singleton();
static NestedSingleton() {}
}
}