Created
May 3, 2016 10:03
-
-
Save nipafx/42f8f30295068204cea734871e20309c to your computer and use it in GitHub Desktop.
Exposing partially constructed instances
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Reordering { | |
private static Constructed constructed; | |
public static void main(String[] args) throws Exception { | |
new Thread(Reordering::constructForever).start(); | |
Thread.sleep(100); | |
checkInitializationUntilFails(); | |
} | |
private static void constructForever() { | |
while (true) { | |
constructed = new Constructed(); | |
} | |
} | |
private static void checkInitializationUntilFails() { | |
while (true) { | |
if (!constructed.initialized) { | |
throw new IllegalStateException(); | |
} | |
} | |
} | |
private static class Constructed { | |
private boolean initialized; | |
public Constructed() { | |
initialized = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work. But if you do what professionals do, it does.
For future reference:
hg clone http://hg.openjdk.java.net/code-tools/jcstress/ jcstress
cd jcstress/
mvn clean install -pl tests-custom -am
UnsafePublication
:java -XX:-UseCompressedOops -jar tests-custom/target/jcstress.jar -t .*UnsafePublication.* -v
My results: