ThreadStatic的C#单例模式设计 [英] C# Singleton Pattern Designs for ThreadStatic

查看:136
本文介绍了ThreadStatic的C#单例模式设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚单例模式的设计.我想为单例类的每个线程创建单独的实例.所以我在下面提供了两种设计.

I want to figure out about singleton pattern designs. I want to create seperated instances for per thread from my singleton class. So I provided two designs below.

正在运行

class Program
{
    static void Main(string[] args)
    {
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Console.ReadLine();
    }
}
public sealed class SingletonClass
{
    [ThreadStatic]
    private static SingletonClass _instance;

    public static SingletonClass Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new SingletonClass();
            }
            return _instance;
        }
    }
    private SingletonClass()
    {

    }
}

它不起作用(抛出NullReferenceException且未创建实例.)

class Program
{
    static void Main(string[] args)
    {
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Console.ReadLine();
    }
}
public sealed class SingletonClass
{
    [ThreadStatic]
    private static SingletonClass _instance = new SingletonClass();

    public static SingletonClass Instance
    {
        get
        {
            return _instance;
        }
    }
    private SingletonClass()
    {

    }
}

我真的很想知道为什么没有为第二个设计创建实例.有人可以解释一下吗?

I am really wondering why an instance is not created for second design. Can anybody explain that please ?

推荐答案

您的问题的答案主要与如何初始化类字段有关.

The answer to your question is mostly related to how class fields are initialized.

在第二个示例中,_instance字段在声明时初始化.每次在声明时初始化静态字段时,都会创建一个静态构造函数(如果尚未声明).在编译时,初始化将被移到静态构造函数中.这意味着您最终将遇到类似这样的情况(由于难以理解,所以没有复制IL代码):

In the second example, the _instance field is initialized at declaration. Every time a static field is initialized at declaration, a static constructor will be created (if you don't have it declared already). At compile time, the initialization will be moved into the static constructor. This means that you will end up having something like this (didn't copy the IL code as it would be harder to understand):

public sealed class SingletonClass
{
    [ThreadStatic]
    private static SingletonClass _instance;

    public static SingletonClass Instance
    {
        get
        {
            return _instance;
        }
    }
    static SingletonClass()
    {
        _instance = new SingletonClass();
    }
}

CLR确保静态构造函数仅被调用一次,而不管您有多少个线程.查看上面的代码,这意味着对于您创建的两个任务,_instance字段将仅初始化一次(因为将仅对静态构造函数进行一次调用).

The CLR ensures that the static constructor is called only once, regardless of how many threads you have. Looking at the above code, it means that for the two Tasks that you created, the _instance field will be initialized only once (since there will be only one call to the static constructor).

这篇关于ThreadStatic的C#单例模式设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆