Created
May 7, 2012 23:00
-
-
Save synther/2631238 to your computer and use it in GitHub Desktop.
Java - Unsafe reference publication
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
class Data { | |
private Object ob1 = new Object(); | |
private Object ob2 = new Object(); | |
private Object ob3 = new Object(); | |
private Object ob4 = new Object(); | |
private Object ob5 = new Object(); | |
private Object ob6 = new Object(); | |
public void check() { | |
if (ob1 == null || | |
ob2 == null || | |
ob3 == null || | |
ob4 == null || | |
ob5 == null || | |
ob6 == null) { | |
System.out.println("fail"); | |
} | |
} | |
} | |
public class Program { | |
private Data sharedData; | |
public static void main(String[] args) { | |
new Program().run(); | |
} | |
private void run() { | |
new Thread(new Runnable() { | |
public void run() { | |
System.out.println("rr()"); | |
while (true) { | |
sharedData = new Data(); | |
} | |
} | |
}).start(); | |
for (int i = 0; i < 50; i++) { | |
new Thread(new Runnable() { | |
public void run() { | |
System.out.println("run()"); | |
while (true) { | |
Data local = sharedData; | |
if (local != null) { | |
local.check(); | |
} | |
} | |
} | |
}).start(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment