[concurrency-interest] Question about "happens-before" andreordering

Jeremy Manson jmanson at cs.purdue.edu
Fri May 19 14:01:03 EDT 2006

Pete Soper wrote:

> Oh, so what I asserted to Tom is wrong. Thanks for this clarification.
> If a finalizer method might run on an object for which construction is 
> incomplete in an arbitrary manner (by virtue of the ctor throwing an 
> exception), it means writing a good finalizer method is more challenging 
> than writing correct concurrent code for the uninitiated, or at least 
> just as challenging. It tells me if I ever get tempted to write a 
> finalizer method I'll put an edge between the last statement of the ctor 
> and the first statement of the finalizer as the first lines of code 
> written. 

Hopefully, you shouldn't be too tempted to write finalizer methods. 
There is a reason we say that there is an edge from the end of the 
constructor to the finalizer.  Uses of the object after completion of 
the constructor are not guaranteed to happen-before the finalizer.  In 
simpler terms, *the finalizer will not necessarily see updates that 
occur to an object after the constructor completes*.  You can set a 
field of that object, and the finalizer might not see it!

To get around this, you need a happens-before edge from the last use of 
the object in the rest of the code to the finalizer.  I urge people 
interested in writing finalizers to read Section 12.6.1 in JLS3.

Actually, I urge people interested in writing finalizers not to write 

 > And it's difficult to reconcile this with Bill Pugh's comment
> about 4034630 on the page Tom pointed to. Is the JLS being updated for 
> Mustang to go along with the implementation change, or am I still not 
> getting it?

I had forgotten to mention that issue.  There is a special case to my 
comments - the constructor for class Object needs to be invoked and 
complete normally for finalization to occur at all.  Other constructors 
may terminate by throwing exceptions.  The details are in JLS section 12.6.

Thanks for the clarification.


More information about the Concurrency-interest mailing list