Java volatile是否阻止缓存或强制执行直写式缓存? [英] Does Java volatile prevent caching or enforce write-through caching?

查看:196
本文介绍了Java volatile是否阻止缓存或强制执行直写式缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解Java的volatile关键字,有关如何在具有CPU缓存的多线程程序中写入.

我已经阅读了一些教程和Java语言规范,尤其是.我的理解是,当线程将新值写入易失性变量时,更新后的值必须对其他读取该变量的线程可见.对我来说,这些语义可以通过以下两种方式之一实现:

  1. 线程可以在CPU高速缓存中高速缓存易失性变量,但是必须立即将对高速缓存中变量的写操作刷新到主内存中.换句话说,缓存是直写.

  2. 线程永远不能缓存易失性变量,而必须在主内存中读写此类变量.

此方法教程( Java中的易变变量"中提到了方法2,此外, 教程表示:

此变量的值永远不会在线程本地缓存:全部 读写将直接进入主存储器"

Java中使用哪种正确的方法?

与Stackoverflow相关的问题,回答了我的问题:

Java中的易变变量

Java volatile读刷新吗进行写入,并进行易失性写入更新读取

Java volatile和缓存一致性

解决方案

保证只是您在语言规范中看到的内容.从理论上讲,写入易失性变量可能会强制将高速缓存刷新到主内存,或者可能不会,随后的读取会强制将高速缓存刷新或以某种方式导致在没有高速缓存刷新的情况下在高速缓存之间进行数据传输.这种模糊性是有意为之的,因为它允许潜在的未来优化,如果更详细地阐明易失性变量的机制,这可能是不可能的.

在实践中,对于当前的硬件,这可能意味着,如果没有连贯的缓存,则写入volatile变量会强制将缓存刷新到主内存.当然,有了一致的缓存,就不需要这种刷新了.

I'm trying to understand Java's volatile keyword with respect to writing to a volatile atomic variable in a multithreaded program with CPU caches.

I've read several tutorials and the Java Language Specification, particularly section 17.4.5 on "happens-before ordering". My understanding is that when a thread writes a new value to a volatile variable, the updated value must be visible to other threads reading that variable. To me, these semantics can be implemented in one of two ways:

  1. Threads can cache a volatile variable in the CPU cache, but a write to the variable in the cache must be flushed immediately to main memory. In other words, the cache is write-through.

  2. Threads can never cache a volatile variable and must read and write such variables in main memory.

Approach 1 is mentioned in this tutorial (http://tutorials.jenkov.com) that says:

By declaring the counter variable volatile all writes to the counter variable will be written back to main memory immediately.

Approach 2 is mentioned in a Stackoverflow question "Volatile variable in Java" and also this tutorial that says:

The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory"

Which one is the correct approach used in Java?

Related Stackoverflow questions which do not answer my question:

Volatile variable in Java

Does Java volatile read flush writes, and does volatile write update reads

Java volatile and cache coherence

解决方案

The guarantees are only what you see in the language specification. In theory, writing a volatile variable might force a cache flush to main memory, or it might not, perhaps with a subsequent read forcing the cache flush or somehow causing transfer of the data between caches without a cache flush. This vagueness is deliberate, as it permits potential future optimizations that might not be possible if the mechanics of volatile variables were spelled out in more detail.

In practice, with current hardware, it probably means that, absent a coherent cache, writing a volatile variable forces a cache flush to main memory. With a coherent cache, of course, such a flush isn't needed.

这篇关于Java volatile是否阻止缓存或强制执行直写式缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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