Singleton模式的实现方式 C#

来源:互联网 发布:东南大学软件测试专业 编辑:程序博客网 时间:2024/06/10 00:31
单例模式标准写法 (注意volatile不能少掉):

public sealed class Singleton{    static <span style="color:#ff0000;">volatile </span>Singleton instance=null;    static readonly object padlock = new object();    Singleton()    {    }    public static Singleton Instance    {        get        {            if (instance==null)            {                lock (padlock)                {                    if (instance==null)                    {                        instance = new Singleton();                    }                }            }            return instance;        }    }}



http://www.yoda.arachsys.com/csharp/singleton.html

http://stackoverflow.com/questions/1964731/the-need-for-volatile-modifier-in-double-checked-locking-in-net



0 0
原创粉丝点击