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