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 volatile help (at least one of the fields) to return performance, or just setting the gap between them?
  • one
    no, volatile will 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.Contended annotation. - zRrr
  • @zRrr I will study an example (I actually met these concepts in the video-analysis jmh), but at this stage it is not clear from the theory, why do performance problems arise at all? in the case when several threads often overwrite the same object (true sharing), and if it is volatile , 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 about volatile and new concepts related to cache lines. - TheSN
  • By the way, is the False sharing effect already taken into account in concurrency classes? and do not worry about it? say, if I turn from different threads to the neighboring elements of the concurrency-collection, in the hope that the necessary indents are automatically added there - TheSN
  • one
    False sharing is bad because it creates traffic on the data bus, because several processors will split one and the same memory area and have to reload it into the cache due to changes that they don’t actually need, but invalidate the cache lines (for example, 8 processors read the first field, but one processor writes to the second and both fields in the same cache line). The effect of false sharing is worthless to consider in all classes. A good example of a class in which it counts LongAdder . - a_gura

0