[jdom-interest] Re: JDOM thread safety

Jason Hunter jhunter at collab.net
Sun Aug 13 18:35:07 PDT 2000


Joseph Bowbeer wrote:
> 
> > I agree, although I'd say "synchronized
> > when necessary"
> 
> If you study the description of why double-check locking is
> broken, I think you'll see that synchronization is necessary most
> of the time.

The article doesn't directly speak to what we're doing, but it does seem
to imply that with the following code:

  trimmedContent = textContent.toString();

Java would permit the trimmedContent variable to be seen by another
thread as non-null and full of default values.  Here's the pertinent
paragraph:

  The most obvious reason it doesn't work it that the writes that 
  initialize the Helper object and the write to the
  helper field can be done or perceived out of order. Thus, a thread
  which invokes getHelper() could see a non-null
  reference to a helper object, but see the default values for 
  fields of the helper object, rather than the values set in the
  constructor. 

If that's true, how does anything in Java work?  :-)  The ability to see
an object before it's fully constructed seems to break general OO
programming behavior.

I don't have time to spare thinking about this right now, so maybe
others want to do their own analysis.  The article is:

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

-jh-



More information about the jdom-interest mailing list