static { // pre-load and initialize InterruptedException and Cleaner classes // so that we don't get into trouble later in the run loop if there's // memory shortage while loading/initializing them lazily. ensureClassInitialized(InterruptedException.class); ensureClassInitialized(Cleaner.class); }
publicvoidrun(){ // 一直将pending 引用链表中的Reference放入ReferenceQueue中 while (true) { tryHandlePending(true); } } } staticbooleantryHandlePending(boolean waitForNotify){ Reference<Object> r; Cleaner c; try { // 加锁,避免同时垃圾回收器操作 synchronized (lock) { if (pending != null) { r = pending; // 'instanceof' might throw OutOfMemoryError sometimes // so do this before un-linking 'r' from the 'pending' chain... // 判断引用是否为 Cleaner c = r instanceof Cleaner ? (Cleaner) r : null; // unlink 'r' from 'pending' chain pending = r.discovered; r.discovered = null; } else { // The waiting on the lock may cause an OutOfMemoryError // because it may try to allocate exception objects. if (waitForNotify) { lock.wait(); } // retry if waited return waitForNotify; } } } catch (OutOfMemoryError x) { // Give other threads CPU time so they hopefully drop some live references // and GC reclaims some space. // Also prevent CPU intensive spinning in case 'r instanceof Cleaner' above // persistently throws OOME for some time... Thread.yield(); // retry returntrue; } catch (InterruptedException x) { // retry returntrue; }
// Fast path for cleaners if (c != null) { c.clean(); // 如果Reference 为Cleaner,则调用其clean方法 returntrue; }
ReferenceQueue<? super Object> q = r.queue; // 将 Rference 放入 RerenceQueue if (q != ReferenceQueue.NULL) q.enqueue(r); returntrue; } static { ThreadGroup tg = Thread.currentThread().getThreadGroup(); for (ThreadGroup tgn = tg; tgn != null; tg = tgn, tgn = tg.getParent()); Thread handler = new ReferenceHandler(tg, "Reference Handler"); /* If there were a special system-only priority greater than * MAX_PRIORITY, it would be used here */ handler.setPriority(Thread.MAX_PRIORITY); // 设置线程优先级为最高 handler.setDaemon(true); // 守护线程 handler.start(); // 启动线程
// provide access in SharedSecrets SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() { @Override publicbooleantryHandlePendingReference(){ return tryHandlePending(false); } }); }
privatestaticclassFinalizerThreadextendsThread{ privatevolatileboolean running; FinalizerThread(ThreadGroup g) { super(g, "Finalizer"); } publicvoidrun(){ // in case of recursive call to run() if (running) return;
// Finalizer thread starts before System.initializeSystemClass // is called. Wait until JavaLangAccess is available while (!VM.isBooted()) { // delay until VM completes initialization try { VM.awaitBooted(); } catch (InterruptedException x) { // ignore and continue } } final JavaLangAccess jla = SharedSecrets.getJavaLangAccess(); running = true; // 循环取出队首元素并执行runFinalizer方法 for (;;) { try { Finalizer f = (Finalizer)queue.remove(); f.runFinalizer(jla); } catch (InterruptedException x) { // ignore and continue } } } } static { ThreadGroup tg = Thread.currentThread().getThreadGroup(); 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(); // 启动线程 }