在Java中,volatile变量是否可以定义为static? [英] Can volatile variable be defined as static in java?

查看:586
本文介绍了在Java中,volatile变量是否可以定义为static?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以声明这样的东西

static volatile boolean first=false;


推荐答案

展开迈克尔的评论。

static 仅仅意味着不与包含类的实例相关联。

static simply means not associated with an instance of the containing class.

volatile 只是意味着该值可能被其他线程更改而没有警告。

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?"

Michael指出,这个问题的答案是肯定的。实例关联与并发修改正交。

As Michael pointed out, the answer to that question is yes. Instance association is orthogonal to concurrent modification.

这篇关于在Java中,volatile变量是否可以定义为static?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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