Top Description Inners Fields Constructors Methods
jnr.ffi.util.ref

public Class FinalizableReferenceQueue

extends Object
Class Inheritance
Imports
java.io.FileNotFoundException, .IOException, java.lang.ref.Reference, .ReferenceQueue, java.lang.reflect.Method, java.net.URL, .URLClassLoader, java.util.Collections, .Map, .WeakHashMap, java.util.logging.Level, .Logger

A reference queue with an associated background thread that dequeues references and invokes FinalizableReference#finalizeReferent() on them.

Keep a strong reference to this object until all of the associated referents have been finalized. If this object is garbage collected earlier, the backing thread will not invoke finalizeReferent() on the remaining references.

Author
Bob Lee
Since
2.0 (imported from Google Collections Library)

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
FinalizableReferenceQueue.DecoupledLoader

Try to load Finalizer in its own class loader.

pack-priv static class
FinalizableReferenceQueue.DirectLoader

Loads Finalizer directly using the current class loader.

pack-priv static interface
pack-priv static class
FinalizableReferenceQueue.SystemLoader

Tries to load Finalizer from the system class loader.

Field Summary

Modifier and TypeField and Description
private static final String
private static final Map<FinalizableReferenceQueue, Boolean>
private static final Logger
pack-priv final ReferenceQueue<Object>
queue

The actual reference queue that our background thread will poll.

private static final Method
startFinalizer

Reference to Finalizer.startFinalizer().

pack-priv final boolean
threadStarted

Whether or not the background thread started successfully.

Constructor Summary

AccessConstructor and Description
public
FinalizableReferenceQueue()

Constructs a new queue.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
cleanUp()

Repeatedly dequeues references from the queue and invokes FinalizableReference#finalizeReferent() on them until the queue is empty.

public static void
pack-priv static Method
getStartFinalizer(Class<?> finalizer)

Looks up Finalizer.startFinalizer().

private static Class<?>

Returns:

Finalizer.class
loadFinalizer
(FinalizableReferenceQueue.FinalizerLoader... loaders)

Iterates through the given loaders until it finds one that can load Finalizer.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FINALIZER_CLASS_NAMEback to summary
private static final String FINALIZER_CLASS_NAME
finalizerQueuesback to summary
private static final Map<FinalizableReferenceQueue, Boolean> finalizerQueues
loggerback to summary
private static final Logger logger
queueback to summary
pack-priv final ReferenceQueue<Object> queue

The actual reference queue that our background thread will poll.

startFinalizerback to summary
private static final Method startFinalizer

Reference to Finalizer.startFinalizer().

threadStartedback to summary
pack-priv final boolean threadStarted

Whether or not the background thread started successfully.

Constructor Detail

FinalizableReferenceQueueback to summary
public FinalizableReferenceQueue()

Constructs a new queue.

Annotations
@SuppressWarnings:unchecked

Method Detail

cleanUpback to summary
pack-priv void cleanUp()

Repeatedly dequeues references from the queue and invokes FinalizableReference#finalizeReferent() on them until the queue is empty. This method is a no-op if the background thread was created successfully.

cleanUpAllback to summary
public static void cleanUpAll()
getStartFinalizerback to summary
pack-priv static Method getStartFinalizer(Class<?> finalizer)

Looks up Finalizer.startFinalizer().

loadFinalizerback to summary
private static Class<?> loadFinalizer(FinalizableReferenceQueue.FinalizerLoader... loaders)

Iterates through the given loaders until it finds one that can load Finalizer.

Returns:Class<?>

Finalizer.class

pollReferenceQueueback to summary
private void pollReferenceQueue()
jnr.ffi.util.ref back to summary

pack-priv Class FinalizableReferenceQueue.DecoupledLoader

extends Object
implements FinalizerLoader
Class Inheritance
All Implemented Interfaces
jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader

Try to load Finalizer in its own class loader. If Finalizer's thread had a direct reference to our class loader (which could be that of a dynamically loaded web application or OSGi bundle), it would prevent our class loader from getting garbage collected.

Field Summary

Modifier and TypeField and Description
private static final String

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv URL
getBaseUrl()

Gets URL for base of path containing Finalizer.class.

public Class<?>
loadFinalizer()

Implements jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader.loadFinalizer.

Returns Finalizer.class or null if this loader shouldn't or can't load it.
pack-priv URLClassLoader
newLoader(URL base)

Creates a class loader with the given base URL as its classpath.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

LOADING_ERRORback to summary
private static final String LOADING_ERROR

Constructor Detail

DecoupledLoaderback to summary
pack-priv DecoupledLoader()

Method Detail

getBaseUrlback to summary
pack-priv URL getBaseUrl() throws IOException

Gets URL for base of path containing Finalizer.class.

loadFinalizerback to summary
public Class<?> loadFinalizer()

Implements jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader.loadFinalizer.

Doc from jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader.loadFinalizer.

Returns Finalizer.class or null if this loader shouldn't or can't load it.

Annotations
@Override
newLoaderback to summary
pack-priv URLClassLoader newLoader(URL base)

Creates a class loader with the given base URL as its classpath.

jnr.ffi.util.ref back to summary

pack-priv Class FinalizableReferenceQueue.DirectLoader

extends Object
implements FinalizerLoader
Class Inheritance
All Implemented Interfaces
jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader

Loads Finalizer directly using the current class loader. We won't be able to garbage collect this class loader, but at least the world doesn't end.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Class<?>
loadFinalizer()

Implements jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader.loadFinalizer.

Returns Finalizer.class or null if this loader shouldn't or can't load it.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DirectLoaderback to summary
pack-priv DirectLoader()

Method Detail

loadFinalizerback to summary
public Class<?> loadFinalizer()

Implements jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader.loadFinalizer.

Doc from jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader.loadFinalizer.

Returns Finalizer.class or null if this loader shouldn't or can't load it.

Annotations
@Override
jnr.ffi.util.ref back to summary

pack-priv Interface FinalizableReferenceQueue.FinalizerLoader

Known Direct Implementers
jnr.ffi.util.ref.FinalizableReferenceQueue.SystemLoader, jnr.ffi.util.ref.FinalizableReferenceQueue.DecoupledLoader, jnr.ffi.util.ref.FinalizableReferenceQueue.DirectLoader

Loads Finalizer.class.

Method Summary

Modifier and TypeMethod and Description
public Class<?>
loadFinalizer()

Returns Finalizer.class or null if this loader shouldn't or can't load it.

Method Detail

loadFinalizerback to summary
public Class<?> loadFinalizer()

Returns Finalizer.class or null if this loader shouldn't or can't load it.

Exceptions
SecurityException:
if we don't have the appropriate privileges
jnr.ffi.util.ref back to summary

pack-priv Class FinalizableReferenceQueue.SystemLoader

extends Object
implements FinalizerLoader
Class Inheritance
All Implemented Interfaces
jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader

Tries to load Finalizer from the system class loader. If Finalizer is in the system class path, we needn't create a separate loader.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Class<?>
loadFinalizer()

Implements jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader.loadFinalizer.

Returns Finalizer.class or null if this loader shouldn't or can't load it.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

SystemLoaderback to summary
pack-priv SystemLoader()

Method Detail

loadFinalizerback to summary
public Class<?> loadFinalizer()

Implements jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader.loadFinalizer.

Doc from jnr.ffi.util.ref.FinalizableReferenceQueue.FinalizerLoader.loadFinalizer.

Returns Finalizer.class or null if this loader shouldn't or can't load it.

Annotations
@Override