Created
May 7, 2013 12:08
-
-
Save betaveros/5532105 to your computer and use it in GitHub Desktop.
An experiment in breaking certain "easy" CS questions about whether an expression can evaluate to "true". (Note that you can get "a != a" to be true without messing with threads (Java Puzzlers, Puzzle 29; left as an exercise for the reader), but this method is far more general; you can get things like "a < 0 && a > 0" to be true, but only someti…
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 Troll { | |
static volatile int a; | |
public static void main(String[] args) { | |
new Thread(new Runnable(){ | |
public void run(){ | |
while (true){ a = -42; a = 1337; } | |
} | |
}).start(); | |
for (int i = 0; i < 1000000; i++){ | |
if (a != a) { | |
System.out.println("!"); | |
System.exit(1); | |
} | |
} | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment