I'm having difficulty understanding the implications of sections 12.6.1 and 12.6.2 of the Java SE 8 Language specification. I am working with a product in which Java objects manage native peers so getting finalization right (until we get a chance to do a ref-queue rewrite) is important.
It is clear, from the spec, that finalizers can be run out of order. It is reachability with which I'm having a hard time.
I believe that the spec says this:
class A {
public Object o = new Object()
protected synchronized void finalize() throws Throwable { o = null; }
}
class B {
A a = new A()
protected void finalize() throws Throwable {
a.getClass() // always works: a cannot be null.
a.o.getClass() // might NPE: a's finalizer might have been run
}
}
class C {
A a = new A()
protected void finalize() throws Throwable {
synchronized (a) {
a.getClass() // always works: a cannot be null.
a.o.getClass() // always works: a.o cannot be null.
}
}
}
There are 4 assertions, above. I would very much appreciate confirmation that they are true, or explanations for why one or more is false.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…