为什么静态字段通常被认为是线程安全的? [英] Why are static fields generally considered threadsafe?

查看:154
本文介绍了为什么静态字段通常被认为是线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个.NET和Java开发谁是比较新的线程。我想知道为什么静态字段,静态构造函数等通常被认为是线程。

I'm a .NET and Java developer who is relatively new to threading. I'd like to know why static fields, static constructors, etc are generally considered "threadsafe".

例如,构建在.NET一个单身的时候,的招数之一,使其线程是使一些模式静态组件。

For example, when constructing a singleton in .NET, one of the tricks to make it threadsafe is to make some of the components of the pattern static.

请让我知道如果我的任何上述假设是不正确的。

Please let me know if any of my above assumptions are incorrect.

推荐答案

我想你感到困惑,这些招数'是不是线程,但避免被初始化线程。所以,如果事情是initilized当应用程序加载后,你不必担心线程具有并初始化的事情时,他们首先问的资源,从而避免了竞争条件,其中线程抢先

I think you are getting confused, those 'tricks' are not about threading, but avoiding initialization by threads. So if something is initilized when the app loads, you don't have to worry about threads having to intialize things when they first ask for the resource, and hence avoid the race condition of which thread gets there first

这篇关于为什么静态字段通常被认为是线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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