简体   繁体   中英

How to implements Java Memory Model Causality test case 1

I'm studying Java memory model in Java Language Specification 17.4.I want to test causality for understanding java memory model.I find a set of causality in JMM causality test case .I implement test case 1, it follows :

@JCStressTest
@State
public class CausalityTestCase1 {

    int x = 0;
    int y = 0;

    @Actor
    public void actor1(IntResult2 r) {
        r.r1 = x;

        if (r.r1 >= 0) {
            y = 1;
        }
    }

    @Actor
    public void actor2(IntResult2 r) {
        r.r2 = y;

        x = r.r2;
    }

}

I used jcstress lib to test this case.r1 == r2 == 1 is impossible,but don't occured.I think my implemention is not corrected, but I don't known how to test.

You are likely running of an Intel architecture, where this racing condition does not occur. VMs can implement a stricter model then what the Java memory model requires, but they are not required to do so. If you used SPARC or ARM, you might discover different outcome. The JMM tries to encompass all these architectures in one model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM