Does JVM guarantee to cache not volatile variable?

后端 未结 3 1324
囚心锁ツ
囚心锁ツ 2021-01-27 00:42

Does JVM guarantee to cache not volatile variable ?

Can a programer depend upon on JVM to always cache non-volatile variables locally for each thread.

Or JVM may

3条回答
  •  遇见更好的自我
    2021-01-27 01:10

    No. The JVM doesn't guarantee "caching" of non-volatile fields. What implementations of JVM guarantee is how volatile fields should behave. Caching of fields is non-standard (unspecified) and can vary from JVM to JVM implementation. So, you shouldn't really rely on it (even if find out, by some way that some data is being cached by a thread)

提交回复
热议问题