I have a static HashMap
that is initialized on server startup. Clients initialize their data from this map when they login.
Now I need to refresh this map, but
First of all your map needs to be declared as volatile
in order to ensure that each thread has the last version of it, then here is how you could proceed:
public void refresh() {
synchronized (MyClass.class) {
Map newMap = prepareData();
map = Collections.unmodifiableMap(newMap);
}
}
And your map would be declared as below:
private static volatile Map map = ...