home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 3.4 KB | 143 lines |
- /*
- * @(#)Finalizer.java 1.10 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.lang.ref;
-
-
- final class Finalizer extends FinalReference { /* Package-private; must be in
- same package as the Reference
- class */
-
- /* A native method that invokes an arbitrary object's finalize method is
- required since the finalize method is protected */
- static native void invokeFinalizeMethod(Object o) throws Throwable;
-
- static private ReferenceQueue queue = new ReferenceQueue();
- static private Finalizer unfinalized = null;
-
- private Finalizer
- next = null,
- prev = null;
- static private Object lock = new Object();
-
- private boolean hasBeenFinalized() {
- return (next == this);
- }
-
- private void add() {
- synchronized (lock) {
- if (unfinalized != null) {
- this.next = unfinalized;
- unfinalized.prev = this;
- }
- unfinalized = this;
- }
- }
-
- private void remove() {
- synchronized (lock) {
- if (unfinalized == this) {
- if (this.next != null) {
- unfinalized = this.next;
- } else {
- unfinalized = this.prev;
- }
- }
- if (this.next != null) {
- this.next.prev = this.prev;
- }
- if (this.prev != null) {
- this.prev.next = this.next;
- }
- this.next = this; /* Indicates that this has been finalized */
- this.prev = this;
- }
- }
-
- private Finalizer(Object finalizee) {
- super(finalizee, queue);
- add();
- }
-
- static void register(Object finalizee) { /* Invoked by VM */
- new Finalizer(finalizee);
- }
-
- synchronized private void runFinalizer() {
- if (!hasBeenFinalized()) {
- try {
- Object finalizee = this.get();
- if (finalizee != null) {
- invokeFinalizeMethod(finalizee);
- finalizee = null;
- }
- } catch (Throwable x) { }
- remove();
- super.clear();
- }
- }
-
- /* Called by Runtime.runFinalization() */
- /* ## REMIND: Finalizers should be run in a clean thread */
- static void runFinalization() {
- for (;;) {
- Finalizer f = (Finalizer)queue.poll();
- if (f == null) break;
- f.runFinalizer();
- }
- }
-
- private static boolean runOnExit = false;
-
- /* Called by Runtime.runFinalizersOnExit */
- static void setRunFinalizersOnExit(boolean value) {
- runOnExit = value;
- }
-
- /* Called on exit by the VM */
- /* ## REMIND: Finalizers should be run in a clean thread */
- static void runFinalizersOnExit() {
- if (! runOnExit) return;
- Finalizer f, nf;
- for (f = unfinalized; f != null; f = nf) {
- nf = f.next;
- f.runFinalizer();
- }
- }
-
- static private Thread finalizer = new FinalizerThread();
-
- private static class FinalizerThread extends Thread {
- public void run() {
- for (;;) {
- try {
- Finalizer f = (Finalizer)queue.remove();
- f.runFinalizer();
- } catch (InterruptedException x) {
- continue;
- }
- }
- }
- }
-
- static {
- finalizer.setName("Finalizer");
- /* ## REMIND: Temporary until finalizer priority-boosting works */
- finalizer.setPriority(Thread.MAX_PRIORITY - 2);
- finalizer.setDaemon(true);
- finalizer.start();
- }
-
- }
-