I declare two non- volatile fields in the class, one changes one thread, the other changes the other. Suppose jvm placed them side by side, so that both fall into a 64-byte cache line.
- Do I understand correctly that this is exactly the case that they call false sharing?
- Will the
volatilehelp (at least one of the fields) to return performance, or just setting the gap between them?
volatilewill not help (on the contrary, it will worsen the performance even more). JMH has an example about false sharing, in particular with the@sun.misc.Contendedannotation. - zRrrvolatile, then the actual value always falls into memory (the necessary cache line returns to its place), and if it isне volatile, then performance should not violated, and there should be a data loss situation. I can not connect in any way what I already know aboutvolatileand new concepts related to cache lines. - TheSNLongAdder. - a_gura