FileDocCategorySizeDatePackage
Finalizer.javaAPI DocJava SE 5 API4647Fri Aug 26 14:57:06 BST 2005java.lang.ref

Finalizer

public final class Finalizer extends FinalReference

Fields Summary
private static ReferenceQueue
queue
private static Finalizer
unfinalized
private static Object
lock
private Finalizer
next
private Finalizer
prev
Constructors Summary
private Finalizer(Object finalizee)

	super(finalizee, queue);
	add();
    
Methods Summary
private voidadd()

	synchronized (lock) {
	    if (unfinalized != null) {
		this.next = unfinalized;
		unfinalized.prev = this;
	    }
	    unfinalized = this;
	}
    
private static voidforkSecondaryFinalizer(java.lang.Runnable proc)

	PrivilegedAction pa = new PrivilegedAction() {
	    public Object run() {
		ThreadGroup tg = Thread.currentThread().getThreadGroup();
		for (ThreadGroup tgn = tg;
		     tgn != null;
		     tg = tgn, tgn = tg.getParent());
		Thread sft = new Thread(tg, proc, "Secondary finalizer");
		sft.start();
		try {
		    sft.join();
		} catch (InterruptedException x) {
		    /* Ignore */
		}
		return null;
	    }};
	AccessController.doPrivileged(pa);
    
private booleanhasBeenFinalized()


       
	return (next == this);
    
static native voidinvokeFinalizeMethod(java.lang.Object o)

static voidregister(java.lang.Object finalizee)

	new Finalizer(finalizee);
    
private voidremove()

	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;
	}
    
static voidrunAllFinalizers()

	forkSecondaryFinalizer(new Runnable() {
	    public void run() {
		for (;;) {
		    Finalizer f;
		    synchronized (lock) {
			f = unfinalized;
			if (f == null) break;
			unfinalized = f.next;
		    }
		    f.runFinalizer();
		}}});
    
static voidrunFinalization()

	forkSecondaryFinalizer(new Runnable() {
	    public void run() {
		for (;;) {
		    Finalizer f = (Finalizer)queue.poll();
		    if (f == null) break;
		    f.runFinalizer();
		}
	    }
	});
    
private voidrunFinalizer()

	synchronized (this) {
	    if (hasBeenFinalized()) return;
	    remove();
	}
	try {
	    Object finalizee = this.get();
	    if (finalizee != null && !(finalizee instanceof java.lang.Enum)) {
		invokeFinalizeMethod(finalizee);
		/* Clear stack slot containing this variable, to decrease
		   the chances of false retention with a conservative GC */
		finalizee = null;
	    }
	} catch (Throwable x) { }
	super.clear();