Skip to content

Instantly share code, notes, and snippets.

@johncarl81
Created February 19, 2012 19:32
Show Gist options
  • Save johncarl81/1865319 to your computer and use it in GitHub Desktop.
Save johncarl81/1865319 to your computer and use it in GitHub Desktop.
Synconrized(Scopes.class):
public class Scopes {
public static <T> T getObject(ScopeSource source, Class<T> clazz, ScopedBuilder<T> builder){
T result = source.getScopedObject(clazz);
if(result == null){
synchronized (Scopes.class){
result = source.getScopedObject(clazz);
if(result == null){
result = source.putScopedObject(clazz, builder.build(source));
}
}
}
return result;
}
}
vs on method parameter:
public class Scopes {
public static <T> T getObject(ScopeSource source, Class<T> clazz, ScopedBuilder<T> builder){
T result = source.getScopedObject(clazz);
if(result == null){
synchronized (source){
result = source.getScopedObject(clazz);
if(result == null){
result = source.putScopedObject(clazz, builder.build(source));
}
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment