Skip to content

Instantly share code, notes, and snippets.

@ptuomaal
Last active December 13, 2015 22:38
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 ptuomaal/4985336 to your computer and use it in GitHub Desktop.
Save ptuomaal/4985336 to your computer and use it in GitHub Desktop.
public class MagicalLand {
public static void main(String[] args) {
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE");
}
}
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE");
}
}
System.out.println("END OF PROGRAM");
}
private static class Unicorn {
private static final ThreadLocal<Integer> line = new ThreadLocal<Integer>();
public static boolean pat() {
StackTraceElement elements[] = Thread.currentThread().getStackTrace();
if ( elements != null ) {
for ( StackTraceElement e : elements ) {
if(e.getClassName().endsWith("MagicalLand") &&
(line.get() == null || line.get() != e.getLineNumber())) {
line.set(e.getLineNumber());
return true;
}
}
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment