instruction-reordering

Can object access be reordered with that object's final field access in Java?

耗尽温柔 提交于 2020-06-25 04:41:08
问题 Below code sample is taken from JLS 17.5 "final Field Semantics": class FinalFieldExample { final int x; int y; static FinalFieldExample f; public FinalFieldExample() { x = 3; y = 4; } static void writer() { f = new FinalFieldExample(); } static void reader() { if (f != null) { int i = f.x; // guaranteed to see 3 int j = f.y; // could see 0 } } } Since the instance of FinalFieldExample is published through a data race, is it possible that the f != null check evaluates successfully, yet

How to demonstrate Java instruction reordering problems?

坚强是说给别人听的谎言 提交于 2020-06-24 02:58:28
问题 With Java instruction reordering the execution order of the code is changed by the JVM at compile time or run time, possibly causing unrelated statements to be executed out-of-order. So my question is: Can someone provide an example Java program/snippet, that reliably shows an instruction reordering problem, that is not caused also by other synchronization issues ( such as caching/visibility or non-atomic r/w, as in my failed attempt at such a demo in my previous question ) To emphasize, I am

How to demonstrate Java instruction reordering problems?

痞子三分冷 提交于 2020-06-24 02:57:21
问题 With Java instruction reordering the execution order of the code is changed by the JVM at compile time or run time, possibly causing unrelated statements to be executed out-of-order. So my question is: Can someone provide an example Java program/snippet, that reliably shows an instruction reordering problem, that is not caused also by other synchronization issues ( such as caching/visibility or non-atomic r/w, as in my failed attempt at such a demo in my previous question ) To emphasize, I am