为什么 volatile 变量通常是私有的?
我有两个类 - 类“MyDerived
”派生自抽象基类“MyBase
”。此外,PrimaryDerivedClass
也继承了MyBase
。 我首先创建 PrimaryDerivedClass
的实例,然后创建“MyDerived
”的实例。
MyBase
有一个名为 ProcessThread
的受保护方法,当 PrimaryDerivedClass
的新实例时,它的使用方式类似于 ThreadPool.QueueUserWorkItem(ProcessThread)
> 并创建了 MyDerived
。现在,当我想在某些情况下停止所有线程时,我将一些布尔 易失性变量设置为 TRUE,并且当任何其他线程找到易失性变量的值 = TRUE 时,它会停止处理。
当我在 MyBase
中声明“private volatile bool stopThreads
”时,我没有得到预期的行为。然而,当我将其设为static
或public
时,我可以看到我想要实现的目标。 如果私有 volatility 会出现什么问题?
抛开上述要求,我还有一个问题: 通常,我看到 volatile 变量被声明为私有。是否有任何重要的方面导致它通常被声明为私有?
(我是多线程的新手。)
I have two classes - Class 'MyDerived
' derives from the abstract base class 'MyBase
'. Also PrimaryDerivedClass
also inherits MyBase
.
I first create the instance of PrimaryDerivedClass
which then creates instances of 'MyDerived
'.
MyBase
has a Protected method called ProcessThread
which is used like ThreadPool.QueueUserWorkItem(ProcessThread)
when new instance of the PrimaryDerivedClass
and MyDerived
is created. Now when i want to stop all the threads in some scenario, I am setting some boolean volatile variable to TRUE and when any other thread find volatile variable's value = TRUE
, it stops processing.
When I declared 'private volatile bool stopThreads
' in MyBase
, I didn't get the intended behavior. However, when I made it static
or public
I could see what I am trying to acheieve.
What would be wrong in case of private volatile?
Keeping the above requirement aside, I have a question further:
Usually, I have seen volatile variable is declared private. Is there any important aspect because of which it's usually declared private?
(I am new to multithreading.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将访问修饰符从 private 更改为 public 不会对易失性字段的行为产生任何影响(至少不会影响易失性)。您还有其他一些问题,在没有看到您的代码的情况下我们无法猜测。
调试多线程很困难,因为代码执行的方式是不确定的 - 您无法准确预测线程何时运行。
事实上,易失性字段是私有的,因为它们被类用于内部同步。它们是其实现的一部分,而不是其接口。
No changing the access modifier from private to public won't have any effect on the behavior of the volatile field (at least not is volatility). You have some other issue, which we can't make a guess on without seeing your code.
Debugging multi-threading is hard since the way the code executes is non-deterministic - you can't predict when precisely your threads will run.
The fact that volatile fields are private is that they are used for synchronization internally by the class. They are part of it's implementation not its interface.
易失性字段是私有的,因为它们是字段,而不是易失性字段。如果需要公开值,请使用属性,它允许您隐藏实现细节。
在这种情况下,也许您可以将
volatile Boolean
更改为ManualResetEvent
,而无需更改任何调用obj.StopThreads = true
的代码。可能有人会说,名为 Stop() 的方法比属性更好。Volatile fields are private because they are fields, not volatile. If you need to expose values, use properties, which allow you to hide implementation details.
In this case, maybe you'll change your
volatile Boolean
into aManualResetEvent
without needing to change any code callingobj.StopThreads = true
. It may be argued that a a method named Stop() would be better than a property all together.这种“奇怪”的行为仅仅是因为访问修饰符。 Private 意味着该变量只能从定义它的类中访问。所有派生类都无法访问它,因此当您更改基类的 volatile 变量的值时,它的更改仅适用于基类内部工作 。
所有从基类继承的类都应该有自己的 stop-right-now 实现。但是,当您将此变量设置为公共(这意味着没有访问限制,可以从任何地方访问它)或静态(这意味着该值对于所有实例都相同,因为它“属于”不属于实例而是属于类并且可以访问时,一切都会发生变化按类名而不是实例)。如果您希望衍生工具可以访问您的变量,您应该将其标记为受保护。
http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx - 本文展示了访问修饰符的含义。
This 'strange' behavior is merely because of the access modifier. Private means that this variable is accessible only from the class it was defined in. All derived classes don't have access to it so when you change base class's volatile variable's value it changes for base class inner works only.
All classes that inherit from the base should have their own implementation of stop-right-now. But everything changes when you set this variable as public (which means no access restrictions, it's accessible from everywhere) or static (which means the value is the same for all instances because it 'belongs' not to an instance but to class and is accessible by class name not by instance). If you want your variable to be accessible for derivatives you should mark it as protected.
http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx - this article shows the meanings of access modifiers.