首页 > 编程 > .NET > 正文

C#多线程Singleton(单件)模式模板

2020-01-18 00:32:32
字体:
来源:转载
供稿:网友
复制代码 代码如下:

private static volatile T _instance = null;
private static object objLock = new Object();
private T()
{
}
public static T Instance
{
get
{
if (_instance == null)
{
lock (objLock)
{
if (_instance == null)
{
_instance = new T();
}
}
}
return _instance;
}
}

在必要的时候需如果要刷新当前instance,可以这样写:
复制代码 代码如下:

public static void RefreshInstance()
{
_instance = new T();
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表