欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-06 08:56:37
复制代码 代码如下: private static volatile t _instance = null; private static object objlock =...
复制代码 代码如下:

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();
}