Suppose our code has 2 threads (A and B) have a reference to the same instance of this class somewhere:
public class MyValueHolder {
private int value = 1;
// ... getter and setter
}
When Thread A does myValueHolder.setValue(7)
, there is no guarantee that Thread B will ever read that value: myValueHolder.getValue()
could - in theory - keep returning 1
forever.
In practice however, the hardware will clear the second level cache sooner or later, so Thread B will read 7
sooner or later (usually sooner).
Is there any way to make the JVM emulate that worst case scenario for which it keeps returning 1
forever for Thread B? That would be very useful to test our multi-threaded code with our existing tests under those circumstances.
jcstress maintainer here. There are multiple ways to answer that question.
-XX:+StressLCM -XX:+StressGCM
, effectively doing the instruction scheduling fuzzing. Chances are the load in question will float somewhere you can detect with the regular tests your product has.