Skip to content

Instantly share code, notes, and snippets.

@betaveros
Created May 7, 2013 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betaveros/5532105 to your computer and use it in GitHub Desktop.
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…
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