Skip to content

Instantly share code, notes, and snippets.

@gweakliem
Created August 13, 2019 02:16
Show Gist options
  • Save gweakliem/e92113f85f375de6d3a718c7e8894d2a to your computer and use it in GitHub Desktop.
Save gweakliem/e92113f85f375de6d3a718c7e8894d2a to your computer and use it in GitHub Desktop.
Interview question: what does this print?
class Scratch {
private static boolean done;
private static int count;
public static void main(String[] args) throws InterruptedException {
done = false;
new Thread(() -> {
System.out.println("Running");
count = 0;
while (!done) {
count++;
}
System.out.println("count = " + count);
}).run();
Thread.sleep(2000);
done = true;
System.out.println("Done!");
}
}
@gweakliem
Copy link
Author

Followup: how long does it take to execute?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment