jmm: Java Memory Model companion code (volatile, happens-before, DCL)

Runnable jcstress + JUnit companion for the ankurm.com post on the
Java Memory Model, double-checked locking, and the volatile fix.
This commit is contained in:
2026-09-30 05:31:29 +00:00
commit f548d9eeb5
22 changed files with 721 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
$ java -jar target/jcstress.jar -t PlainPublicationTest -jvmArgs "-Xmx256m" -f 2 -iters 15 -time 1000
RUN RESULTS:
Interesting tests: No matches.
Failed tests: No matches.
Error tests: No matches.
All remaining tests: 1 matching test results.
.......... [OK] com.ankurm.jmm.PlainPublicationTest
Results across all configurations:
RESULT SAMPLES FREQ EXPECT DESCRIPTION
0, 0 3,511,549,619 66.79% Acceptable Reader ran before the write was published at all.
0, 1 0 0.00% Interesting Reordering: the reference was visible before both of its ...
1, 0 0 0.00% Interesting Reordering: the reference was visible before both of its ...
1, 1 1,745,742,499 33.21% Acceptable Reader saw the fully-constructed Payload.
+21
View File
@@ -0,0 +1,21 @@
$ java -jar target/jcstress.jar -t VolatilePublicationTest -jvmArgs "-Xmx256m" -f 2 -iters 15 -time 1000
RUN RESULTS:
Interesting tests: No matches.
Failed tests: No matches.
Error tests: No matches.
All remaining tests: 1 matching test results.
.......... [OK] com.ankurm.jmm.VolatilePublicationTest
Results across all configurations:
RESULT SAMPLES FREQ EXPECT DESCRIPTION
0, 0 4,290,607,784 80.26% Acceptable Reader ran before the write was published at all.
0, 1 0 0.00% Forbidden Would mean the volatile happens-before edge failed to hold.
1, 0 0 0.00% Forbidden Would mean the volatile happens-before edge failed to hold.
1, 1 1,055,076,014 19.74% Acceptable Reader saw the fully-constructed Payload.
+21
View File
@@ -0,0 +1,21 @@
$ java -jar target/jcstress.jar -t FinalFieldPublicationTest -jvmArgs "-Xmx256m" -f 2 -iters 15 -time 1000
RUN RESULTS:
Interesting tests: No matches.
Failed tests: No matches.
Error tests: No matches.
All remaining tests: 1 matching test results.
.......... [OK] com.ankurm.jmm.FinalFieldPublicationTest
Results across all configurations:
RESULT SAMPLES FREQ EXPECT DESCRIPTION
0, 0 3,697,560,129 66.13% Acceptable Reader ran before the write was published at all.
0, 1 0 0.00% Forbidden Would mean the final-field safe-publication guarantee fai...
1, 0 0 0.00% Forbidden Would mean the final-field safe-publication guarantee fai...
1, 1 1,893,699,349 33.87% Acceptable Reader saw the fully-constructed FinalPayload.
@@ -0,0 +1,19 @@
$ javac -cp jcstress-core.jar -d out -s gen PlainPublicationTest.java Payload.java # JDK 21.0.10, no -processorpath
Note: Annotation processing is enabled because one or more processors were found
on the class path. A future release of javac may disable annotation processing
unless at least one processor is specified by name (-processor), or a search
path is specified (--processor-path, --processor-module-path), or annotation
processing is enabled explicitly (-proc:only, -proc:full).
Use -Xlint:-options to suppress this message.
Use -proc:none to disable annotation processing.
$ find gen -type f
gen/com/ankurm/jmm/PlainPublicationTest_jcstress.java
$ javac -cp jcstress-core.jar -d out -s gen PlainPublicationTest.java Payload.java # JDK 25.0.4.1+1, no -processorpath
(no output at all - exit code 0)
$ find gen -type f
(nothing - the processor was never invoked, no error, no warning)
+6
View File
@@ -0,0 +1,6 @@
$ mvn -q test -pl jmm
-------------------------------------------------------------------------------
Test set: com.ankurm.jmm.SingletonBehaviorTest
-------------------------------------------------------------------------------
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.067 s -- in com.ankurm.jmm.SingletonBehaviorTest