Skip to content

Instantly share code, notes, and snippets.

@dain
Last active Dec 15, 2015
Embed
What would you like to do?
public class MyMutableObject
{
private int value;
public MyMutableObject(int value)
{
this.value = value;
}
public synchronized int getValue()
{
return value;
}
public static MyMutableObject object;
public static void main(String[] args)
{
for (int i = 0; i < 1000; i++) {
new Thread(new Runnable() {
@Override
public void run()
{
while (true) {
MyMutableObject instance = object;
if (instance != null && instance.getValue() == 0) {
System.out.println("The impossible happened");
}
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}
}).start();
}
object = new MyMutableObject(42);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment