1
@JCStressTest 
@Outcome(id = "1, 1", expect = Expect.ACCEPTABLE, desc = "ordered") 
@Outcome(id = "0, 1", expect = Expect.ACCEPTABLE, desc = "ordered") 
@Outcome(id = "1, 0", expect = Expect.ACCEPTABLE, desc = "reordered") 
@Outcome(id = "0, 0", expect = Expect.ACCEPTABLE, desc = "ordered") 
@State 
public class TestJUC {
     private int x;

    public TestJUC() {}

    @Actor
    public void actor1() {
     x = 1;
    }

    @Actor
    public void actor2(II_Result r) {


        1.int a = x;
        2.r.r1 = a;

        3.int b = x;
        4.r.r2 = b;

    }
}

After passing the jcstress test, there will be no result of (1,0). In the actor 2 method, in order to ensure as-if-serial, statement 1 must be before statement 2 and statement 3 must be before statement 4 after reordering.

The following situation is possible.

int b = x;

int a = x;

r.r1 = a;

r.r2 = b;

This situation may result in (1,0), but after jctress testing, this result did not occur. Why is this?

3
  • How are you invoking to the JVM? Are you including the VM options described at jfeatures.com/blog/jcstress? Commented Sep 28, 2024 at 7:38
  • 2
    The explanation may be that something in the stress test introduces a serendipitous "happens before" chain. Or maybe it just introduces an unrelated memory barrier that prevents reordering. The JMM only talks about when memory effects are guaranteed to be visible. It does provide any guarantees about when they are not visible. Commented Sep 28, 2024 at 7:45
  • Also something in your computer or the JVM could decide it wouldn't be worth doing that reordering (at least on the machine you are testing on when you were testing that). Commented Sep 28, 2024 at 18:57

1 Answer 1

0

The reordering is allowed, but in the current shape it won't happen. Keep in mind that the JIT isn't obligated to demonstrate all possible behaviors.

The code needs to be made a bit more complicated to trick the JIT into triggering this behavior.

https://github.com/openjdk/jcstress/blob/master/jcstress-samples/src/main/java/org/openjdk/jcstress/samples/jmm/basic/BasicJMM_05_Coherence.java

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.