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.
As an example of how this is used, imagine you have a class MyServer that creates a
ServerSocket, and you would like to ensure that the ServerSocket is closed even if the MyServer object is garbage-collected without calling
its close method. You could use a finalizer to accomplish this, but that has a
number of well-known problems. Here is how you might use this class instead:
public class MyServer implements Closeable {
private static final FinalizableReferenceQueue frq = new FinalizableReferenceQueue();
// You might also share this between several objects.
private static final Set<Reference<?>> references = Sets.newConcurrentHashSet();
// This ensures that the FinalizablePhantomReference itself is not garbage-collected.
private final ServerSocket serverSocket;
private MyServer(...) {
...
this.serverSocket = new ServerSocket(...);
...
}
public static MyServer create(...) {
MyServer myServer = new MyServer(...);
final ServerSocket serverSocket = myServer.serverSocket;
Reference<?> reference = new FinalizablePhantomReference<MyServer>(myServer, frq) {
public void finalizeReferent() {
references.remove(this):
if (!serverSocket.isClosed()) {
...log a message about how nobody called close()...
try {
serverSocket.close();
} catch (IOException e) {
...
}
}
}
};
references.add(reference);
return myServer;
}
public void close() {
serverSocket.close();
}
}
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | FinalizableReferenceQueue.
Try to load Finalizer in its own class loader. |
| pack-priv static class | FinalizableReferenceQueue.
Loads Finalizer directly using the current class loader. |
| pack-priv static interface | FinalizableReferenceQueue.
Loads Finalizer.class. |
| pack-priv static class | FinalizableReferenceQueue.
Tries to load Finalizer from the system class loader. |
| Modifier and Type | Field and Description |
|---|---|
| private static final String | |
| pack-priv final PhantomReference | |
| private static final Logger | |
| pack-priv final ReferenceQueue | 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. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | cleanUp()
Repeatedly dequeues references from the queue and invokes |
| public void | close()
Implements java. |
| pack-priv static Method | |
| private static Class | Returns: Finalizer.classIterates through the given loaders until it finds one that can load Finalizer. |
| FINALIZER_CLASS_NAME | back to summary |
|---|---|
| private static final String FINALIZER_CLASS_NAME | |
| frqRef | back to summary |
|---|---|
| pack-priv final PhantomReference<Object> frqRef | |
| logger | back to summary |
|---|---|
| private static final Logger logger | |
| queue | back to summary |
|---|---|
| pack-priv final ReferenceQueue<Object> queue The actual reference queue that our background thread will poll. | |
| startFinalizer | back to summary |
|---|---|
| private static final Method startFinalizer Reference to Finalizer.startFinalizer(). | |
| threadStarted | back to summary |
|---|---|
| pack-priv final boolean threadStarted Whether or not the background thread started successfully. | |
| FinalizableReferenceQueue | back to summary |
|---|---|
| public FinalizableReferenceQueue() Constructs a new queue. | |
| cleanUp | back to summary |
|---|---|
| pack-priv void cleanUp() Repeatedly dequeues references from the queue and invokes | |
| close | back to summary |
|---|---|
| public void close() Implements java. Doc from java. Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect. As noted in
| |
| getStartFinalizer | back to summary |
|---|---|
| pack-priv static Method getStartFinalizer(Class<?> finalizer) Looks up Finalizer.startFinalizer(). | |
| loadFinalizer | back to summary |
|---|---|
| private static Class Iterates through the given loaders until it finds one that can load Finalizer.
| |
| Modifier and Type | Field and Description |
|---|---|
| private static final String |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv URL | |
| public Class | loadFinalizer()
Implements com. |
| pack-priv URLClassLoader |
| LOADING_ERROR | back to summary |
|---|---|
| private static final String LOADING_ERROR | |
| DecoupledLoader | back to summary |
|---|---|
| pack-priv DecoupledLoader() | |
| getBaseUrl | back to summary |
|---|---|
| pack-priv URL getBaseUrl() throws IOException Gets URL for base of path containing Finalizer.class. | |
| loadFinalizer | back to summary |
|---|---|
| public Class Implements com. Doc from com. Returns Finalizer.class or null if this loader shouldn't or can't load it.
| |
| newLoader | back to summary |
|---|---|
| pack-priv URLClassLoader newLoader(URL base) Creates a class loader with the given base URL as its classpath. | |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public Class | loadFinalizer()
Implements com. |
| DirectLoader | back to summary |
|---|---|
| pack-priv DirectLoader() | |
| loadFinalizer | back to summary |
|---|---|
| public Class Implements com. Doc from com. Returns Finalizer.class or null if this loader shouldn't or can't load it.
| |
| Modifier and Type | Method and Description |
|---|---|
| public Class |
| loadFinalizer | back to summary |
|---|---|
| public Class Returns Finalizer.class or null if this loader shouldn't or can't load it.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static boolean |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public Class | loadFinalizer()
Implements com. |
| disabled | back to summary |
|---|---|
| pack-priv static boolean disabled
| |
| SystemLoader | back to summary |
|---|---|
| pack-priv SystemLoader() | |
| loadFinalizer | back to summary |
|---|---|
| public Class Implements com. Doc from com. Returns Finalizer.class or null if this loader shouldn't or can't load it.
| |