Let's say I have the following (suppose there are no generics for java 1.4):
public class CacheManager {
static HashMap states;
static boolean statesLoaded;
public static String getState(String abbrev) {
if(!statesLoaded) {
loadStates();
}
return (String) states.get(abbrev);
}
private static void loadStates() {
statesLoaded = true;
}
}
In a multi-threaded high-load environment, such as a web application server, a problem could theoretically occur if> 1 thread tries to retrieve and load the cache at the same time. (Assuming there is no startup code in the web application to initialize the cache)
Is using Collections.synchronizedMap just enough to fix it? Does the returned synchronizedMap have performance issues when executing get () if many threads are accessing it?
HashMap load ? , , .
, , 2 getStates(), , stateLoaded . , true. , , statesLoaded , , . ?