RE: JavaMemoryModel: Memory model and finalize methods

From: Hudson, Rick (rick.hudson@intel.com)
Date: Tue Dec 14 1999 - 11:30:39 EST


Bill asks: "What is the appropriate set of rules to describe
the interaction of the memory model and finalizers?"

The appropriate rules have to be the same as the rules for
any other concurrent access to a shared variable. The thread
that does the finalization should expect to obey the same
rules as any other thread accessing potentially shared
variables. If reordering is allowed by the memory model
semantics when there is no finalization then it should be
be allowed when there is finalization. This is a weaker
statement than what Bill suggests.

Concerning Tom Cargill's comment relative to weak references:
We have the same issues for any weak reference construct
except for a weak reference semantics that do not allow
access to the object once it is not strongly reachable.

- Rick Hudson

-----Original Message-----
From: Bill Pugh [mailto:pugh@cs.umd.edu]
Sent: Monday, December 13, 1999 2:26 PM
To: javaMemoryModel@cs.umd.edu
Subject: JavaMemoryModel: Memory model and finalize methods

What is the appropriate set of rules to describe
the interaction of the memory model and finalizers?

Say a thread creates an object A, performs some writes
to object A, and then drops the reference to A. When the
finalizer for A is run, is it guaranteed to see all of
the writes to A?

Say a thread creates an object A, performs some writes
to object A, and B, and then drops the reference to A. When the
finalizer for A is run, is it guaranteed to see all of
the writes to both A and B?

Here is an example below:
class A {
  B x;
  protected void finalize() {
        System.out.println(x.y);
        }
  }

public class B {
  int y;
  public static void main(String args[]) {
        A a = new A();
        B b = new B();
        a.x = b;
        b.y = 42;
        a = null;
        }
  }

If we wanted to guarantee that the finalizer would see all
writes to both a and b, then reordering the statements b.y = 42
and a = null would be illegal.

My suggestion:

  When a finalizer is run, it is guaranteed to see all of the writes
to the object being finalized, but not to any other object.

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

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



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