JavaMemoryModel: Memory model and finalize methods

From: Bill Pugh (pugh@cs.umd.edu)
Date: Mon Dec 13 1999 - 17:26:10 EST


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



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