Skip to content

Instantly share code, notes, and snippets.

@patrykpoborca
Created August 22, 2015 01:44
Show Gist options
  • Save patrykpoborca/b42bb8d1fb53a72e7c40 to your computer and use it in GitHub Desktop.
Save patrykpoborca/b42bb8d1fb53a72e7c40 to your computer and use it in GitHub Desktop.
@SuppressWarnings("unchecked") // cast only happens when result comes from the factory
@Override
public T get() {
// double-check idiom from EJ2: Item 71
Object result = instance;
if (result == UNINITIALIZED) {
synchronized (this) {
result = instance;
if (result == UNINITIALIZED) {
instance = result = factory.get();
}
}
}
return (T) result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment