2009-07-22 00:57:55 +00:00
|
|
|
public class Finalizers {
|
2009-08-24 23:51:31 +00:00
|
|
|
private static final Object lock = new Object();
|
2009-07-22 00:57:55 +00:00
|
|
|
private static boolean finalized = false;
|
|
|
|
|
|
|
|
private static void expect(boolean v) {
|
|
|
|
if (! v) throw new RuntimeException();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void finalize() {
|
2009-08-24 23:51:31 +00:00
|
|
|
synchronized (lock) {
|
|
|
|
finalized = true;
|
|
|
|
lock.notifyAll();
|
|
|
|
}
|
2009-07-22 00:57:55 +00:00
|
|
|
}
|
|
|
|
|
2009-08-24 23:51:31 +00:00
|
|
|
public static void main(String[] args) throws Exception {
|
2009-07-22 00:57:55 +00:00
|
|
|
new Finalizers();
|
|
|
|
|
|
|
|
expect(! finalized);
|
|
|
|
|
2009-08-24 23:51:31 +00:00
|
|
|
synchronized (lock) {
|
|
|
|
System.gc();
|
|
|
|
lock.wait(5000);
|
|
|
|
}
|
2009-07-22 00:57:55 +00:00
|
|
|
|
|
|
|
expect(finalized);
|
|
|
|
|
|
|
|
new Finalizers2();
|
|
|
|
|
|
|
|
finalized = false;
|
|
|
|
|
|
|
|
expect(! finalized);
|
|
|
|
|
2009-08-24 23:51:31 +00:00
|
|
|
synchronized (lock) {
|
|
|
|
System.gc();
|
|
|
|
lock.wait(5000);
|
|
|
|
}
|
2009-07-22 00:57:55 +00:00
|
|
|
|
|
|
|
expect(finalized);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class Finalizers2 extends Finalizers { }
|
|
|
|
|
|
|
|
}
|