[concurrency-interest] Question about Java Memory Model

Eric Zoerner eric.zoerner@gemstone.com
Mon, 15 Dec 2003 09:35:11 -0800


We are seeing some bizarre behavior in the ConcurrentHashMap where one thread 
constructs an objects and puts it into a CHM and another thread gets the object 
and sees it as uninitialized. My question has to do with the Java Memory Model 
and volatile fields. It looks like the CHM attempts to protect against the 
scenario just described by writing to the count field of the segment, which is a 
volatile field. The intent seems to be that this will invoke a memory barrier 
which will flush _all_ pending writes in the thread working set to memory. Is 
this correct?

I could not find any information that would make that guarantee, however, either 
in the Java Language Spec or in Concurrent Programming in Java. Those references 
say that writing to a volatile field only flushes that one variable to memory, 
not all pending writes.

The object being constructed and then seen as  uninitialized is, by the way, 
another instance of ConcurrentHashMap.

How can this scenario occur?  Is synchronization necessary to make sure newly 
constructed objects put into a CHM are visible to other threads as fully 
initialized?

Eric Zoerner
GemStone Systems.
http://www.gemstone.com