Skip to content

Instantly share code, notes, and snippets.

@prydin
Last active December 19, 2019 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prydin/3b1461cc6609d8dc1d73cc133d0024be to your computer and use it in GitHub Desktop.
Save prydin/3b1461cc6609d8dc1d73cc133d0024be to your computer and use it in GitHub Desktop.
Bad way of doing double check
class BrokenDontUse {
private Map<String, SomeResource> aHashMap = new HashMap<>();
public SomeResource getResource(String name) {
SomeResource r = aHashMap.get(name);
if(r != null) {
return r;
}
synchronized(aHashMap) {
SomeResource r = aHashMap.get(name);
if(r != null) {
return r;
}
r = new SomeResource(name);
aHashMap.put(name, r)
return r;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment