Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2008 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.base.internal;
 
Thread that finalizes referents. All references should implement com.google.common.base.FinalizableReference.

While this class is public, we consider it to be *internal* and not part of our published API. It is public so we can access it reflectively across class loaders in secure environments.

This class can't depend on other Google Collections code. If we were to load this class in the same class loader as the rest of Google Collections, this thread would keep an indirect strong reference to the class loader and prevent it from being garbage collected. This poses a problem for environments where you want to throw away the class loader. For example, dynamically reloading a web application or unloading an OSGi bundle.

com.google.common.base.FinalizableReferenceQueue loads this class in its own class loader. That way, this class doesn't prevent the main class loader from getting garbage collected, and this class can detect when the main class loader has been garbage collected and stop itself.

 
 public class Finalizer implements Runnable {
 
   private static final Logger logger
       = Logger.getLogger(Finalizer.class.getName());

  
Name of FinalizableReference.class.
 
   private static final String FINALIZABLE_REFERENCE
       = "com.google.common.base.FinalizableReference";

  
Starts the Finalizer thread. FinalizableReferenceQueue calls this method reflectively.

Parameters:
finalizableReferenceClass FinalizableReference.class.
queue a reference queue that the thread will poll.
frqReference a phantom reference to the FinalizableReferenceQueue, which will be queued either when the FinalizableReferenceQueue is no longer referenced anywhere, or when its close() method is called.
 
   public static void startFinalizer(
       Class<?> finalizableReferenceClass,
       ReferenceQueue<Objectqueue,
       PhantomReference<ObjectfrqReference) {
     /*
      * We use FinalizableReference.class for two things:
      *
      * 1) To invoke FinalizableReference.finalizeReferent()
      *
      * 2) To detect when FinalizableReference's class loader has to be garbage
      * collected, at which point, Finalizer can stop running
      */
     if (!finalizableReferenceClass.getName().equals()) {
       throw new IllegalArgumentException(
           "Expected " +  + ".");
     }
 
     Finalizer finalizer = new Finalizer(finalizableReferenceClassqueuefrqReference);
     Thread thread = new Thread(finalizer);
     thread.setName(Finalizer.class.getName());
     thread.setDaemon(true);
 
     try {
       if ( != null) {
         .set(threadnull);
       }
     } catch (Throwable t) {
       .log(."Failed to clear thread local values inherited"
           + " by reference finalizer thread."t);
     }
 
     thread.start();
  }
  private final PhantomReference<ObjectfrqReference;
  private final ReferenceQueue<Objectqueue;
  private static final Field inheritableThreadLocals
Constructs a new finalizer thread.
  private Finalizer(
      Class<?> finalizableReferenceClass,
      ReferenceQueue<Objectqueue,
      PhantomReference<ObjectfrqReference) {
    this. = queue;
        = new WeakReference<Class<?>>(finalizableReferenceClass);
    // Keep track of the FRQ that started us so we know when to stop.
    this. = frqReference;
  }

  
Loops continuously, pulling references off the queue and cleaning them up.
  @SuppressWarnings("InfiniteLoopStatement")
  public void run() {
    while (true) {
      try {
        if (!cleanUp(.remove())) {
          break;
        }
      } catch (InterruptedException e) { /* ignore */ }
    }
  }

  
Cleans up a single reference. Catches and logs all throwables.

Returns:
true if the caller should continue, false if the associated FinalizableReferenceQueue is no longer referenced.
  private boolean cleanUp(Reference<?> reference) {
    Method finalizeReferentMethod = getFinalizeReferentMethod();
    if (finalizeReferentMethod == null) {
      return false;
    }
    do {
      /*
       * This is for the benefit of phantom references. Weak and soft
       * references will have already been cleared by this point.
       */
      reference.clear();
      if (reference == ) {
        /*
         * The client no longer has a reference to the
         * FinalizableReferenceQueue. We can stop.
         */
        return false;
      }
      try {
        finalizeReferentMethod.invoke(reference);
      } catch (Throwable t) {
        .log(."Error cleaning up after reference."t);
      }
      /*
       * Loop as long as we have references available so as not to waste
       * CPU looking up the Method over and over again.
       */
    } while ((reference = .poll()) != null);
    return true;
  }

  
Looks up FinalizableReference.finalizeReferent() method.
    Class<?> finalizableReferenceClass
    if (finalizableReferenceClass == null) {
      /*
       * FinalizableReference's class loader was reclaimed. While there's a
       * chance that other finalizable references could be enqueued
       * subsequently (at which point the class loader would be resurrected
       * by virtue of us having a strong reference to it), we should pretty
       * much just shut down and make sure we don't keep it alive any longer
       * than necessary.
       */
      return null;
    }
    try {
      return finalizableReferenceClass.getMethod("finalizeReferent");
    } catch (NoSuchMethodException e) {
      throw new AssertionError(e);
    }
  }
  public static Field getInheritableThreadLocalsField() {
    try {
      Field inheritableThreadLocals
          = Thread.class.getDeclaredField("inheritableThreadLocals");
      inheritableThreadLocals.setAccessible(true);
      return inheritableThreadLocals;
    } catch (Throwable t) {
      .log(."Couldn't access Thread.inheritableThreadLocals."
          + " Reference finalizer threads will inherit thread local"
          + " values.");
      return null;
    }
  }
New to GrepCode? Check out our FAQ X