1 7 8 package java.lang.ref; 9 10 import java.security.PrivilegedAction ; 11 import java.security.AccessController ; 12 13 14 final class Finalizer extends FinalReference { 17 18 21 static native void invokeFinalizeMethod(Object o) throws Throwable ; 22 23 static private ReferenceQueue queue = new ReferenceQueue (); 24 static private Finalizer unfinalized = null; 25 static private Object lock = new Object (); 26 27 private Finalizer 28 next = null, 29 prev = null; 30 31 private boolean hasBeenFinalized() { 32 return (next == this); 33 } 34 35 private void add() { 36 synchronized (lock) { 37 if (unfinalized != null) { 38 this.next = unfinalized; 39 unfinalized.prev = this; 40 } 41 unfinalized = this; 42 } 43 } 44 45 private void remove() { 46 synchronized (lock) { 47 if (unfinalized == this) { 48 if (this.next != null) { 49 unfinalized = this.next; 50 } else { 51 unfinalized = this.prev; 52 } 53 } 54 if (this.next != null) { 55 this.next.prev = this.prev; 56 } 57 if (this.prev != null) { 58 this.prev.next = this.next; 59 } 60 this.next = this; 61 this.prev = this; 62 } 63 } 64 65 private Finalizer(Object finalizee) { 66 super(finalizee, queue); 67 add(); 68 } 69 70 71 static void register(Object finalizee) { 72 new Finalizer (finalizee); 73 } 74 75 private void runFinalizer() { 76 synchronized (this) { 77 if (hasBeenFinalized()) return; 78 remove(); 79 } 80 try { 81 Object finalizee = this.get(); 82 if (finalizee != null && !(finalizee instanceof java.lang.Enum )) { 83 invokeFinalizeMethod(finalizee); 84 86 finalizee = null; 87 } 88 } catch (Throwable x) { } 89 super.clear(); 90 } 91 92 105 private static void forkSecondaryFinalizer(final Runnable proc) { 106 PrivilegedAction pa = new PrivilegedAction () { 107 public Object run() { 108 ThreadGroup tg = Thread.currentThread().getThreadGroup(); 109 for (ThreadGroup tgn = tg; 110 tgn != null; 111 tg = tgn, tgn = tg.getParent()); 112 Thread sft = new Thread (tg, proc, "Secondary finalizer"); 113 sft.start(); 114 try { 115 sft.join(); 116 } catch (InterruptedException x) { 117 118 } 119 return null; 120 }}; 121 AccessController.doPrivileged(pa); 122 } 123 124 125 static void runFinalization() { 126 forkSecondaryFinalizer(new Runnable () { 127 public void run() { 128 for (;;) { 129 Finalizer f = (Finalizer )queue.poll(); 130 if (f == null) break; 131 f.runFinalizer(); 132 } 133 } 134 }); 135 } 136 137 138 static void runAllFinalizers() { 139 forkSecondaryFinalizer(new Runnable () { 140 public void run() { 141 for (;;) { 142 Finalizer f; 143 synchronized (lock) { 144 f = unfinalized; 145 if (f == null) break; 146 unfinalized = f.next; 147 } 148 f.runFinalizer(); 149 }}}); 150 } 151 152 private static class FinalizerThread extends Thread { 153 FinalizerThread(ThreadGroup g) { 154 super(g, "Finalizer"); 155 } 156 public void run() { 157 for (;;) { 158 try { 159 Finalizer f = (Finalizer )queue.remove(); 160 f.runFinalizer(); 161 } catch (InterruptedException x) { 162 continue; 163 } 164 } 165 } 166 } 167 168 static { 169 ThreadGroup tg = Thread.currentThread().getThreadGroup(); 170 for (ThreadGroup tgn = tg; 171 tgn != null; 172 tg = tgn, tgn = tg.getParent()); 173 Thread finalizer = new FinalizerThread(tg); 174 finalizer.setPriority(Thread.MAX_PRIORITY - 2); 175 finalizer.setDaemon(true); 176 finalizer.start(); 177 } 178 179 } 180 | Popular Tags |