java中的volatile变量可以定义为static吗?

发布于 2024-10-27 12:13:48 字数 82 浏览 1 评论 0原文

我可以声明这样的事情吗?

static volatile boolean first=false;

Can I declare something like this??

static volatile boolean first=false;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回忆追雨的时光 2024-11-03 12:13:48

扩展迈克尔的评论。

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.

如此安好 2024-11-03 12:13:48

是的,你可以。

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 it static 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).

只涨不跌 2024-11-03 12:13:48

当然。两个修改器的效果是完全正交的。

Sure. The effects of the two modifiers are completely orthogonal.

缘字诀 2024-11-03 12:13:48

是的,你可以。

static boolean first=false;

当你声明一个静态变量时,无论创建了多少个类的对象,所有对象都只有一份副本。
每个线程都有自己的缓存副本。

如果它是易失性的,线程每次都应该去内存并获取变量的更新值。

static volatile boolean first=false;

它会

强制线程每次直接读取内存

先找到变量的值。

yes, you can.

static boolean first=false;

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.

static volatile boolean first=false;

It will

force the thread to read each time the memory directly

to find the value of the variable first.

ぃ双果 2024-11-03 12:13:48

根据问题,是的,我们可以,但在这种情况下,我们需要处理所有情况。

通过多个线程访问静态值,每个线程都可以拥有其本地缓存副本!为了避免这种情况,您可以将变量声明为静态易失性,这将强制线程每次读取全局值。但是,易失性并不能替代正确的同步

private static 易失性 int cnt= 0;

private void checkCnt() {
     cnt+= 1;
     // some conditions
     // some code
     cnt -= 1;
}

多次并发执行checkCnt最终cnt的值与我们期望的不一样!为了解决这个问题,我们必须实现一个锁来跟踪增量/减量。

private static final Object lock = new Object();
private static volatile int cnt= 0;

private void checkCnt() {
     synchronized (lock) {
         cnt += 1;
     }
     //some conditions
     //some code
     synchronized (lock) {
         cnt -= 1;
     }
}

不确定这是否是正确的方法,但通过这个我们可以管理事物并将静态与易失性结合使用。
如果有更好的方法,请提供意见。

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;

private void checkCnt() {
     cnt+= 1;
     // some conditions
     // some code
     cnt -= 1;
}

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

private static final Object lock = new Object();
private static volatile int cnt= 0;

private void checkCnt() {
     synchronized (lock) {
         cnt += 1;
     }
     //some conditions
     //some code
     synchronized (lock) {
         cnt -= 1;
     }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文