java.lang.ref.Finalizer

來源:互聯網
上載者:User

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

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.