Skip to content

Instantly share code, notes, and snippets.

@dain
Last active December 15, 2015 11:09
Show Gist options
  • Save dain/5250924 to your computer and use it in GitHub Desktop.
Save dain/5250924 to your computer and use it in GitHub Desktop.
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