Created
August 13, 2019 02:16
-
-
Save gweakliem/e92113f85f375de6d3a718c7e8894d2a to your computer and use it in GitHub Desktop.
Interview question: what does this print?
This file contains hidden or 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
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!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Followup: how long does it take to execute?