Created
August 30, 2012 09:08
-
-
Save sherman/3524616 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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