package java.lang.ref;
import java.security.PrivilegedAction;
import java.security.AccessController;
// comment by liqiang
final class Finalizer extends FinalReference { //包內使用的類
/*
* 一個本地方法 用來調用任意Object對象的finalize方法,因為Object對象的finalize方法
* 為protected,不能直接調用
*/
static native void invokeFinalizeMethod(Object o) throws Throwable;
//引用隊列,為static
static private ReferenceQueue queue = new ReferenceQueue();
static private Finalizer unfinalized = null;
//鎖對象
static private Object lock = new Object();
//對象的上一對象,和下一對象,隊列使用雙向
private Finalizer
next = null,
prev = null;
//是否Finalize
private boolean hasBeenFinalized() {
return (next == this);
}
private void add() {
synchronized (lock) {
if (unfinalized != null) {
//如果unfinalized不為null將當前對象加到unfinalized前
this.next = unfinalized;
unfinalized.prev = this;
}
//將unfinalized置為當前對象
unfinalized = this;
}
}
//出隊
private void remove() {
synchronized (lock) {
if (unfinalized == this) {
if (this.next != null) {
unfinalized = this.next;
} else {
unfinalized = this.prev;
}
}
//取出this
if (this.next != null) {
this.next.prev = this.prev;
}
if (this.prev != null) {
this.prev.next = this.next;
}
//將當前對象的next,prev都置成當前對象
this.next = this;
this.prev = this;
}
}
//finalizee為referent
private Finalizer(Object finalizee) {
super(finalizee, queue);
add();
}
//由虛擬機器調用
static void register(Object finalizee) {
//隊列為static的,所以構造的國政直接加入到對列中
new Finalizer(finalizee);
}
//調用封裝對象的finalize方法
private void runFinalizer() {
synchronized (this) {
if (hasBeenFinalized()) return;
remove();
}
try {
//讀取當前對象封裝的將引用對象
Object finalizee = this.get();
if (finalizee != null) {
//調用封裝對象的finalize方法
invokeFinalizeMethod(finalizee);
finalizee = null;
}
} catch (Throwable x) { }
super.clear();
}
private static void forkSecondaryFinalizer(final 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);
}
// 由Runtime.runFinalization()方法來調用
static void runFinalization() {
forkSecondaryFinalizer(new Runnable() {
public void run() {
for (;;) {
//出隊
Finalizer f = (Finalizer)queue.poll();
if (f == null) break;
//調用封裝對象的finalize方法
f.runFinalizer();
}
}
});
}
//由java.lang.Shutdown調用
//調用unfinalized代表的列表的所有對象的強引用的finalize方法
static void runAllFinalizers() {
forkSecondaryFinalizer(new Runnable() {
public void run() {
for (;;) {
Finalizer f;
synchronized (lock) {
f = unfinalized;
if (f == null) break;
unfinalized = f.next;
}
f.runFinalizer();
}}});
}
//一個繼承自線程的內部類
private static class FinalizerThread extends Thread {
FinalizerThread(ThreadGroup g) {
super(g, "Finalizer");
}
public void run() {
for (;;) {//反覆調用隊列的remvoe方法得到對象,並調用相應的runFinalizer方法
try {
Finalizer f = (Finalizer)queue.remove();
f.runFinalizer();
} catch (InterruptedException x) {
continue;
}
}
}
}
static {
//得到當前線程組
ThreadGroup tg = Thread.currentThread().getThreadGroup();
//得到System線程組
for (ThreadGroup tgn = tg;
tgn != null;
tg = tgn, tgn = tg.getParent());
//啟動線程
Thread finalizer = new FinalizerThread(tg);
finalizer.setPriority(Thread.MAX_PRIORITY - 2);
finalizer.setDaemon(true);
finalizer.start();
}
}