I've read about double-checked locking and its drawbacks, but I'm asking if using a synchronizedMap
can be considered safe.
Here is my code:
public class EntityUtils
{
private static final Map<String, Map<String, String>> searchMap = Collections.synchronizedMap(new HashMap<String, Map<String, String>>());
private static Map<String, String> getSearchablePathMap(String key)
{
Map<String, String> pathMap = searchMap.get(key);
if(pathMap != null) return pathMap;
synchronized(searchMap)
{
// double check locking (safe for synchronizedMap?)
pathMap = searchMap.get(key);
if(pathMap != null) return pathMap;
pathMap = new HashMap<>();
pathMap.put(..., ...);
...
// heavy map population operations
...
pathMap = Collections.unmodifiableMap(pathMap);
searchMap.put(key, pathMap);
}
return pathMap;
}
}
Suggestions or improvements are welcome.