只读静态字段怎么可能为空?
下面是我的一个类的摘录:
[ThreadStatic]
readonly static private AccountManager _instance = new AccountManager();
private AccountManager()
{
}
static public AccountManager Instance
{
get { return _instance; }
}
如您所见,它是每个线程的单例 - 即该实例用 ThreadStatic 属性标记。该实例也作为静态构造的一部分进行实例化。
既然如此,当我尝试使用 Instance 属性时,我怎么可能在 ASP.NET MVC 应用程序中遇到 NullReferenceException?
So here's an excerpt from one of my classes:
[ThreadStatic]
readonly static private AccountManager _instance = new AccountManager();
private AccountManager()
{
}
static public AccountManager Instance
{
get { return _instance; }
}
As you can see, it's a singleton-per-thread - i.e. the instance is marked with the ThreadStatic attribute. The instance is also instantiated as part of static construction.
So that being the case, how is it possible that I'm getting a NullReferenceException in my ASP.NET MVC application when I try to use the Instance property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
引用 MSDN ThreadStaticAttribute:
Quoting MSDN ThreadStaticAttribute:
这是 ThreadStatic 属性中令人困惑的部分。尽管它为每个线程创建一个值,但初始化代码仅在其中一个线程上运行。访问该值的所有其他线程都将获得该类型的默认值,而不是初始化代码的结果。
不要将值初始化,而是将其包装在为您执行初始化的属性中。
由于值
_instance
对于每个线程都是唯一的,因此属性中不需要锁定,并且可以像任何其他延迟初始化值一样对待它。This is a confusing part of the
ThreadStatic
attribute. Even though it creates a value per thread, the initalization code is only run on one of the threads. All of the other threads which access this value will get the default for that type instead of the result of the initialization code.Instead of value initialization, wrap it in a property that does the initialization for you.
Because the value
_instance
is unique per thread, no locking is necessary in the property and it can be treated like any other lazily initialized value.您在这里遇到了经典的
[ThreadStatic]
“101”。静态初始化器只会触发一次,即使它被标记为
[ThreadStatic]
,所以其他线程(除了第一个)将看到它未初始化。You've hit a classic
[ThreadStatic]
"101" here.The static initializer will only fire once, even though it is marked as
[ThreadStatic]
, so other threads (apart from the first) will see this uninitialised.我相信发生的情况是静态字段仅被初始化一次,因此当另一个线程尝试读取该字段时它将为空(因为它是默认值),因为 _instance 无法再次初始化。这只是一个想法,但我可能完全偏离了,但这就是我认为正在发生的事情。
I believe what is happening is that the static field is only being initialized once so when another thread tries to read the field it will be null (since its the default value) because _instance can't be initialized again. Its just a thought but and I could be totally way off but that's what I think is happening.
用 ThreadStaticAttribute 标记的静态字段不在线程之间共享。每个执行线程都有一个单独的字段实例,并独立设置和获取该字段的值。如果在不同的线程上访问该字段,它将包含不同的值。
A static field marked with ThreadStaticAttribute is not shared between threads. Each executing thread has a separate instance of the field, and independently sets and gets values for that field. If the field is accessed on a different thread, it will contain a different value.