这可能是一个基础问题。
在多线程环境中,我们可以使用锁来实现单例模式。请参考以下代码片段。但是为什么需要在单例模式中使用双重检查锁定?更重要的是,双重检查锁定是什么意思?
class singleton
{
private static singleton instance = null;
private static singleton() { }
private static object objectlock = new object();
public static singleton Instance
{
get
{
lock (objectlock) //single - check lock
{
if (instance == null)
{
instance = new singleton();
}
return instance;
}
}
}
}