java中的volatile变量可以定义为static吗?
我可以声明这样的事情吗?
static volatile boolean first=false;
Can I declare something like this??
static volatile boolean first=false;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
扩展迈克尔的评论。
static
只是意味着不与包含类的实例关联。易失性只是意味着该值可能会被其他线程更改而不会发出警告。
因此,您的问题归结为“是否可以在没有警告的情况下由另一个线程更改与包含类的实例不关联的字段?”
正如迈克尔指出的那样,这个问题的答案是肯定的。实例关联与并发修改正交。
To expand on Michael's comment.
static
simply means not associated with an instance of the containing class.volatile
simply means that the value may be changed by other threads without warning.So your question boils down to "can a field not associated with an instance of the containing class be changed by another thread without warning?"
As Michael pointed out, the answer to that question is yes. Instance association is orthogonal to concurrent modification.
是的,你可以。
Java 中的静态变量每个类存储一次(而不是像非静态变量那样每个对象存储一次)。这意味着所有对象(和静态方法)共享相同的变量。
将变量声明为
易失性
(无论是否静态
)表明该变量将被多个线程频繁访问。在 Java 中,这归结为指示线程不能缓存变量的值,但必须在变异后立即写回,以便其他线程看到更改。 (默认情况下,Java 中的线程可以自由缓存变量)。Yes, you can.
A
static
variable in Java is stored once per class (not once per object, such as non-static variables are). This means all your objects (and static methods) share the same variable.Declaring a variable as
volatile
(be itstatic
or not) states that the variable will be accessed frequently by multiple threads. In Java, this boils down to instructing threads that they can not cache the variable's value, but will have to write back immediately after mutating so that other threads see the change. (Threads in Java are free to cache variables by default).当然。两个修改器的效果是完全正交的。
Sure. The effects of the two modifiers are completely orthogonal.
是的,你可以。
当你声明一个静态变量时,无论创建了多少个类的对象,所有对象都只有一份副本。
每个线程都有自己的缓存副本。
如果它是易失性的,线程每次都应该去内存并获取变量的更新值。
它会
先找到变量的值。
yes, you can.
When you are declaring a static variable, there will be only one copy for all the objects, no matter how many objects of the class are created.
Each thread would have its own cached copy.
If it is volatile, Thread should go to memory each time and gets the updated value of the variable.
It will
to find the value of the variable first.
根据问题,是的,我们可以,但在这种情况下,我们需要处理所有情况。
通过多个线程访问静态值,每个线程都可以拥有其本地缓存副本!为了避免这种情况,您可以将变量声明为静态易失性,这将强制线程每次读取全局值。但是,易失性并不能替代正确的同步
private static 易失性 int cnt= 0;
多次并发执行checkCnt最终cnt的值与我们期望的不一样!为了解决这个问题,我们必须实现一个锁来跟踪增量/减量。
不确定这是否是正确的方法,但通过这个我们可以管理事物并将静态与易失性结合使用。
如果有更好的方法,请提供意见。
As per the question, yes we can but in that case we need to handle all the scenario.
Access a static value through multiple threads, each thread can have it’s local cached copy! To avoid this you can declare the variable as static volatile and this will force the thread to read each time the global value. However, volatile is not a substitute for proper synchronisation
private static volatile int cnt= 0;
Executing checkCnt concurrently many times the final value of cnt different from what we expect! To solve the problem, we’ve to implement a lock to keep track of increment/decrement
Not sure is this the right way but by this we can manage the things and use static with volatile.
Please provide inputs, if there is better way.