run java finalizers in a separate thread to guarantee no application locks are held when doing so

This commit is contained in:
Joel Dice
2009-08-24 17:51:31 -06:00
parent d4e2e05b31
commit 4297fa04b3
4 changed files with 135 additions and 43 deletions

View File

@ -1,4 +1,5 @@
public class Finalizers {
private static final Object lock = new Object();
private static boolean finalized = false;
private static void expect(boolean v) {
@ -6,15 +7,21 @@ public class Finalizers {
}
protected void finalize() {
finalized = true;
synchronized (lock) {
finalized = true;
lock.notifyAll();
}
}
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
new Finalizers();
expect(! finalized);
System.gc();
synchronized (lock) {
System.gc();
lock.wait(5000);
}
expect(finalized);
@ -24,7 +31,10 @@ public class Finalizers {
expect(! finalized);
System.gc();
synchronized (lock) {
System.gc();
lock.wait(5000);
}
expect(finalized);
}