When run()
method runs in new thread, it will have its own stack. How are the variables (like countdown
in this case) from the main thread accessed by the run method in different stack?
final CountDownLatch countdown = new CountDownLatch(1);
for (int i = 0; i < 10; ++ i) {
Thread racecar = new Thread() {
public void run() {
countdown.await(); //all threads waiting
System.out.println("Vroom!");
}
};
racecar.start();
}
System.out.println("Go");
countdown.countDown();