Saturday, 17 August 2013

Cache synchronization

Cache synchronization

I'm working on a thread safe cache implementation.
Could you please tell me if I make too much synchronization here:
private static final Map<String, Object> cache = new HashMap<String,
Object>();
public static Object getCached(String key) {
Object cached = cache.get(key);
if (cached == null) {
synchronized (cache) {
cached = cache.get(src);
if (cached == null) {
cached = createObject();
cache.put(src, cached);
}
}
}
return cached;
}

No comments:

Post a Comment