RE: JavaMemoryModel: Finalization idioms

From: David Holmes (
Date: Sun May 01 2005 - 05:58:17 EDT


> I believe it works either way. You can also use a
> synchronized(this){} at the end
> of every ordinary method call which might be the last call on the
> object. And it hides this mess inside the object with
> the finalizer.

I don't see how this is the case. If the object's own methods perform the
sync block then that only ensures that the object remains reachable during
execution of that method. If the client uses the object via a local then the
client must ensure there is no premature finalization.

  void someMethod() {
    ResourceManager man = new ResourceManager();
    int result1 = man.useA();
    int result2 = man.useB();

If the ResourceManager finalizer clears resources A and B then the caller of
someMethod needs to prevent premature finalization of the ResourceManager
instance. If useA and useB include a sync block at the end that only ensures
that the finalizer will run either before useA, between useA and useB, or
after useB.

My understanding of this issue is that the client code - that which is
referencing the object - must do something to ensure the object remains
reachable, but following some protocol involving the finalize method of the
object concerned.

David Holmes

JavaMemoryModel mailing list -

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