1. /*
  2. * @(#)Finalizer.java 1.17 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang.ref;
  11. import java.security.PrivilegedAction;
  12. import java.security.AccessController;
  13. final class Finalizer extends FinalReference { /* Package-private; must be in
  14. same package as the Reference
  15. class */
  16. /* A native method that invokes an arbitrary object's finalize method is
  17. required since the finalize method is protected
  18. */
  19. static native void invokeFinalizeMethod(Object o) throws Throwable;
  20. static private ReferenceQueue queue = new ReferenceQueue();
  21. static private Finalizer unfinalized = null;
  22. static private Object lock = new Object();
  23. private Finalizer
  24. next = null,
  25. prev = null;
  26. private boolean hasBeenFinalized() {
  27. return (next == this);
  28. }
  29. private void add() {
  30. synchronized (lock) {
  31. if (unfinalized != null) {
  32. this.next = unfinalized;
  33. unfinalized.prev = this;
  34. }
  35. unfinalized = this;
  36. }
  37. }
  38. private void remove() {
  39. synchronized (lock) {
  40. if (unfinalized == this) {
  41. if (this.next != null) {
  42. unfinalized = this.next;
  43. } else {
  44. unfinalized = this.prev;
  45. }
  46. }
  47. if (this.next != null) {
  48. this.next.prev = this.prev;
  49. }
  50. if (this.prev != null) {
  51. this.prev.next = this.next;
  52. }
  53. this.next = this; /* Indicates that this has been finalized */
  54. this.prev = this;
  55. }
  56. }
  57. private Finalizer(Object finalizee) {
  58. super(finalizee, queue);
  59. add();
  60. }
  61. /* Invoked by VM */
  62. static void register(Object finalizee) {
  63. new Finalizer(finalizee);
  64. }
  65. private void runFinalizer() {
  66. synchronized (this) {
  67. if (hasBeenFinalized()) return;
  68. remove();
  69. }
  70. try {
  71. Object finalizee = this.get();
  72. if (finalizee != null) {
  73. invokeFinalizeMethod(finalizee);
  74. /* Clear stack slot containing this variable, to decrease
  75. the chances of false retention with a conservative GC */
  76. finalizee = null;
  77. }
  78. } catch (Throwable x) { }
  79. super.clear();
  80. }
  81. /* Create a privileged secondary finalizer thread in the system thread
  82. group for the given Runnable, and wait for it to complete.
  83. This method is used by both runFinalization and runFinalizersOnExit.
  84. The former method invokes all pending finalizers, while the latter
  85. invokes all uninvoked finalizers if on-exit finalization has been
  86. enabled.
  87. These two methods could have been implemented by offloading their work
  88. to the regular finalizer thread and waiting for that thread to finish.
  89. The advantage of creating a fresh thread, however, is that it insulates
  90. invokers of these methods from a stalled or deadlocked finalizer thread.
  91. */
  92. private static void forkSecondaryFinalizer(final Runnable proc) {
  93. PrivilegedAction pa = new PrivilegedAction() {
  94. public Object run() {
  95. ThreadGroup tg = Thread.currentThread().getThreadGroup();
  96. for (ThreadGroup tgn = tg;
  97. tgn != null;
  98. tg = tgn, tgn = tg.getParent());
  99. Thread sft = new Thread(tg, proc, "Secondary finalizer");
  100. sft.start();
  101. try {
  102. sft.join();
  103. } catch (InterruptedException x) {
  104. /* Ignore */
  105. }
  106. return null;
  107. }};
  108. AccessController.doPrivileged(pa);
  109. }
  110. /* Called by Runtime.runFinalization() */
  111. static void runFinalization() {
  112. forkSecondaryFinalizer(new Runnable() {
  113. public void run() {
  114. for (;;) {
  115. Finalizer f = (Finalizer)queue.poll();
  116. if (f == null) break;
  117. f.runFinalizer();
  118. }
  119. }
  120. });
  121. }
  122. /* Invoked by java.lang.Shutdown */
  123. static void runAllFinalizers() {
  124. forkSecondaryFinalizer(new Runnable() {
  125. public void run() {
  126. for (;;) {
  127. Finalizer f;
  128. synchronized (lock) {
  129. f = unfinalized;
  130. if (f == null) break;
  131. unfinalized = f.next;
  132. }
  133. f.runFinalizer();
  134. }}});
  135. }
  136. private static class FinalizerThread extends Thread {
  137. FinalizerThread(ThreadGroup g) {
  138. super(g, "Finalizer");
  139. }
  140. public void run() {
  141. for (;;) {
  142. try {
  143. Finalizer f = (Finalizer)queue.remove();
  144. f.runFinalizer();
  145. } catch (InterruptedException x) {
  146. continue;
  147. }
  148. }
  149. }
  150. }
  151. static {
  152. ThreadGroup tg = Thread.currentThread().getThreadGroup();
  153. for (ThreadGroup tgn = tg;
  154. tgn != null;
  155. tg = tgn, tgn = tg.getParent());
  156. Thread finalizer = new FinalizerThread(tg);
  157. finalizer.setPriority(Thread.MAX_PRIORITY - 2);
  158. finalizer.setDaemon(true);
  159. finalizer.start();
  160. }
  161. }