synchronization and HashMaps
I have a HashMap called sCache. 2 or more threads may call put on this map.
public class A
{
public Object someMethod (StringBuffer buffer)
{
Object o = sCache.get(buffer.toString());
if (o == null) {
// Need to create value for this key and cache it
// createValue method is just for illustration purposes
o = createValue(buffer);
sCache.put(buffer.toString(), o);
}
return o;
}
private static Map sCache = new HashMap();
}
My question is about threading and the memory model. If i have no sychronization around this sCache when does one thread see another's put?
My understanding is that each thread could or would hold a local reference to this map so therefore for thread communication purposes we would only need to synchronize if the Map reference were originally null and one thread were instantiating the HashMap? That isn't the case here. Since the actual object referred to by the sCache reference never changes in this example, would i need to synchronize?

