简体   繁体   English

如果直接从内存中读取易失性字段,那么从哪里读取非易失性字段?

[英]If volatile fields are read directly from memory, where are non-volatile fields read from?

I was recently reading volatile fields are thread-safe because 我最近在阅读volatile字段是线程安全的,因为

When we use volatile keyword with a variable, all the threads read its value directly from the memory and don't cache it 当我们将volatile关键字与变量一起使用时,所有线程都直接从内存中读取其值,并且不对其进行缓存

So it got me to wonder, for non-volatile fields, where would the thread read its value from? 因此,我想知道,对于非易失性字段,线程将从何处读取其值? I thought everything was stored in memory. 我以为一切都存储在内存中。

The statement you quote is a misconception. 您引用的陈述是一种误解。 I recommend the following article: https://software.rajivprab.com/2018/04/29/myths-programmers-believe-about-cpu-caches/ 我推荐以下文章: https : //software.rajivprab.com/2018/04/29/myths-programmers-believe-about-cpu-caches/

if volatile variables were truly written/read from main-memory every single time, they would be horrendously slow – main-memory references are 200x slower than L1 cache references. 如果易失性变量每次真正地从主内存中真正写入/读取,它们的速度将非常惊人–主内存引用比L1缓存引用慢200倍。 In reality, volatile-reads (in Java) can often be just as cheap as a L1 cache reference, putting to rest the notion that volatile forces reads/writes all the way to main memory. 实际上,易失性读取(在Java中)通常与L1高速缓存引用一样便宜,从而搁浅了易失性强制一直对主内存进行读写的概念。 If you've been avoiding the use of volatiles because of performance concerns, you might have been a victim of the above misconceptions. 如果由于性能问题而一直避免使用挥发物,则可能是上述误解的受害者。

Every thread has a Cache of the Main Memory. 每个线程都有一个主内存缓存。

A field may be declared volatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable 字段可以声明为volatile,在这种情况下,Java内存模型可确保所有线程看到的变量值均一致

For Example we have this static counter that is not Thread Safe 例如,我们有这个静态计数器不是线程安全的

static int counter = 0;

If two threads reads and write this variable it will be like this 如果两个线程读取并写入此变量,它将像这样

Main memory  - > static int counter 
T1-> Cache of the Main Memory. Reads/Writes directly from Cache
T2-> Cache of the Main Memory. Reads/Writes directly from Cache 

So it will be inconsistent when reading and writing. 因此在阅读和书写时会不一致。

static volatile int counter = 0; 静态volatile int计数器= 0;

Main Memory - > static int counter;
T1 - > Read and Writes directly from the memory
T2 - > Read and Writes directly from the memory

I hope I give you a simple summary about this. 希望我给你一个简单的总结。 Because you will need to check more about concurrency and Atomic Access for the concurrency of variables Check more in the Java Docs 因为您将需要检查有关并发性和原子访问的更多信息以了解变量的并发性,请参阅Java Docs中的更多内容。

Non volatile variables are usually stored in processor cache ( per thread ) and are only periodically updated in main memory. 非易失性变量通常存储在处理器高速缓存中( 每个线程 ),并且仅在主存储器中定期更新。

The volatile modifier means that the variable data is stored in main memory every time it changes and hence is always up to date when another thread needs to access/edit it. volatile修饰符意味着变量数据每次更改都存储在主存储器中,因此当另一个线程需要访问/编辑它时,该变量数据始终是最新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 易读和非易失性字段 - Volatile read and non-volatile fields 线程何时从主内存而不是工作内存中读取非易失性变量? - When does Thread read a non-volatile variable from main memory instead of working memory? 在从另一个线程读取期间是否可以读取非易失性变量的陈旧值? - Is it possible to read stale value of non-volatile variable during read from another thread? 了解Java中的易失性和非易失性读/写 - Understanding volatile and non-volatile read/write in Java compareAndSwap 一个普通成员(非易失性成员)仍然具有易失性读写的内存语义 - compareAndSwap a common member ( non-volatile member ) still has memory semantics of volatile read and write 非易失性字段+来自另一个线程的第一个对象访问(java) - Non-volatile fields + first object access from another thread (java) 易失性和非易失性字段混合时发生易失性关系 - Volatile happens-before relationship when there's mix of volatile and non-volatile fields 对非易失性字段的同步访问是否安全? - Is synchronized access to non-volatile fields thread safe? 发生在与Java中的易失性字段和同步块的关系之前 - 以及它们对非易失性变量的影响? - Happens-before relationships with volatile fields and synchronized blocks in Java - and their impact on non-volatile variables? 混合挥发性和非挥发性 - Mixing volatile and non-volatile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM