Re: JavaMemoryModel: Issue with final fields

From: Bill Pugh (pugh@cs.umd.edu)
Date: Thu Feb 07 2002 - 17:02:56 EST


At 8:18 PM -0800 2/2/02, Cliff Click wrote:
>Well, why the heck not? You publish a Bogon, you die. You call a library
>routine, which publishes a Bogon and the compiler inlines and exposes ...
>you die. Lesson: don't publish Bogons or call upon suspect libraries which
>publish Bogons or ... you occasionally eat a NULL you didn't expect.
>
>Please don't punish Java performance in the name of saving us from Bogons.
>You're asking me to turn off CSE.

The problem is that there is far too much existing code that publish
references to objects during the construction of that object. Any
class that starts a thread within the constructor will typically
expose the incompletely constructed object. Any class that creates
instances of inner classes in their constructor will store references
to the outer object into the heap before the outer object is
completely constructed (whether or not this is publishing the object
is a tricky question).

Given the design of Java, there is often no way to get around this
(other than to mandate the use of factories). Sometimes, objects do
need to have associated threads. Doug, who knows better, had some
examples of such objects in his JSR-166 slides.

So I'm trying to find idioms that allow you to safety publish an
object with final fields during the construction of the object. These
idioms would require that all initialization be done before the
object is published, and that proper synchronization be used.

For all of the cases I care about, the code would be correctly
synchronized if field's weren't declared final. I really want to
minimize or eliminate the number of non-contrived cases where
changing a field from normal to final introduces a synchronization
error.

Any attempt to do this results in a situation where you can have some
references to an object that are incorrectly used, and some that are
correctly used. So the question is, will an incorrect use of an
pre-construction published object cause all uses of that object to be
incorrect?

I think I can fashion something so that you would never to turn off
CSE for a situation where you couldn't do CSE if the field wasn't
final. But for situations where the special final properties of the
field allow you do to CSE where you couldn't normally, there may be
special restrictions.

        Bill
-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:38 EDT