In fact, you don't need an atomic boolean with the following code:
public class YourClass() {
volatile boolean initialized = false;
public void ensureInitialized() {
if ( initialized ) return;
synchronized(this) {
if (!initialized) {
initialize();
initialized = true;
}
}
}
// The code of this method could be moved
// into the synchronized statement above
public void initialize() { ... };
}
Since the initialization code is only going to be called once, there is no real benefit to using an AtomicBoolean.
Synchronizing on 'this' may take a bit more time, but so does the creation of an AtomicBoolean. Both operations happen only once.
Overall, this solution uses less memory.
EDIT: Updated solution