Skip to content

Instantly share code, notes, and snippets.

@sherman
Created August 30, 2012 09:08
Show Gist options
  • Save sherman/3524616 to your computer and use it in GitHub Desktop.
Save sherman/3524616 to your computer and use it in GitHub Desktop.
public class Thunk<T> {
private volatile T value;
private final Provider<T> provider;
public Thunk(Provider<T> provider) {
this.provider = provider;
}
/**
* Data race is possible here: there can be multiple invocations of creator func
*/
public T get() {
if (null == value) {
value = provider.get();
}
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment