本文介绍了Java易失性和缓存一致性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在硬件级别实现缓存一致性,为什么我们需要volatile?任何核心/处理器都应该获得最新价值?

If cache coherency is implemented at hardware level why do we need volatile? Any core/processor should get the latest value anyway?

或者它是否完全处理不同的问题?

Or is it dealing with a different issue completely?

推荐答案

高速缓存一致性可以在处理器级实现,但除非保证了顺序一致性(大多数现代架构都不是这样),如果你要求它,你只能获得缓存一致性。

Cache coherence may be implemented at the processor level but, unless the processor memory model guarantees sequential consistency (which is not the case on most modern architectures), you will only get cache coherence if you ask for it.

这就是volatile的用途:它要求JVM生成相关的机器指令,要求处理器将其缓存与主存储器同步。

That is what volatile is for: it asks the JVM to produce the relevant machine instruction(s) that will ask the processor(s) to synchronize its cache with main memory.

这篇关于Java易失性和缓存一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:57