[concurrency-interest] static initializers and threads

Joshua Bloch josh at bloch.us
Fri May 26 00:47:34 EDT 2006


Now the quiz.  What does this program print, and why?

public class Lazy {
    private static boolean initialized = false;
    static {
        Thread t = new Thread(new Runnable() {
            public void run() {
                initialized = true;
            }
        });
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            throw new AssertionError(e);
        }
    }
    public static void main(String[] args) {
        System.out.println(initialized);
    }
}

For the answer, see Puzzle 85 in "Java Puzzlers."



More information about the Concurrency-interest mailing list