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