标签:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
单例模式
*
注意多线程下的问题
*/
namespace App_MYCS.HDL_SJMS.DLMS_1
{
class my_DLMS_1
{
public void dy()
{
my_dlmsclass dl = my_dlmsclass.getInstance();
}
}
class my_dlmsclass
{
private static my_dlmsclass instance;
private my_dlmsclass()
{
}
public static my_dlmsclass getInstance()
{
if (instance == null)
{
instance = new my_dlmsclass();
}
return instance;
}
}
//--------------------------------------------------------------
//增加线程的安全
class my_dlmsclass1
{
private static my_dlmsclass1 instance;
private static readonly object syncRoot = new object();
private my_dlmsclass1()
{
}
public static my_dlmsclass1 getInstance()
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
instance = new my_dlmsclass1();
}
}
}
return instance;
}
}
//--------------------------------------------------------------
//NET实现静态初始化
sealed class my_dlmsclass2
{
//readonly 通过第一次运行后来确定常量的数据
private static readonly my_dlmsclass2 instance;
private my_dlmsclass2()
{
}
public static my_dlmsclass2 getInstance()
{
return instance;
}
}
}
标签:
原文地址:http://www.cnblogs.com/longkai178/p/5815078.html