mirror of
https://github.com/corda/corda.git
synced 2025-01-06 05:04:20 +00:00
3787985b25
This implementation does not conform to the Java standard in that finalize methods are called from whichever thread happens to be garbage collecting, and that thread may hold locks, whereas the standard guarantees that finalize will be run from a thread which holds no locks. Also, an object will never be finalized more than once, even if its finalize method "rescues" (i.e. makes reachable) the object such that it might become unreachable a second time and thus a candidate for finalization once more. It's not clear to me from the standard if this is OK or not. Nonwithstanding the above, this implementation is useful for "normal" finalize methods which simply release resources associated with an object.
35 lines
545 B
Java
35 lines
545 B
Java
public class Finalizers {
|
|
private static boolean finalized = false;
|
|
|
|
private static void expect(boolean v) {
|
|
if (! v) throw new RuntimeException();
|
|
}
|
|
|
|
protected void finalize() {
|
|
finalized = true;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
new Finalizers();
|
|
|
|
expect(! finalized);
|
|
|
|
System.gc();
|
|
|
|
expect(finalized);
|
|
|
|
new Finalizers2();
|
|
|
|
finalized = false;
|
|
|
|
expect(! finalized);
|
|
|
|
System.gc();
|
|
|
|
expect(finalized);
|
|
}
|
|
|
|
private static class Finalizers2 extends Finalizers { }
|
|
|
|
}
|