Java Reference
In-Depth Information
< Day Day Up >
Puzzle 85: Lazy Initialization
This poor little class is too lazy to initialize itself in the usual way, so it calls on the help of
background thread. What does the program print? Is it guaranteed to print the same thing every time
you run it?
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);
}
}
 
 
Search WWH ::




Custom Search