PhantomReferenceT


PhantomReference<T> java.lang.ref

Java 1.2

This class represents a reference to an object that does not prevent the referent object from being finalized by the garbage collector. When (or at some point after) the garbage collector determines that there are no more hard (direct) references to the referent object, that there are no SoftReference or WeakReference objects that refer to the referent, and that the referent has been finalized, it enqueues the PhantomReference object on the ReferenceQueue specified when the PhantomReference was created. This serves as notification that the object has been finalized and provides one last opportunity for any required cleanup code to be run.

To prevent a PhantomReference object from resurrecting its referent object, its get( ) method always returns null , both before and after the PhantomReference is enqueued. Nevertheless, a PhantomReference is not automatically cleared when it is enqueued, so when you remove a PhantomReference from a ReferenceQueue , you must call its clear( ) method or allow the PhantomReference object itself to be garbage-collected .

This class provides a more flexible mechanism for object cleanup than the finalize( ) method does. Note that in order to take advantage of it, it is necessary to subclass PhantomReference and define a method to perform the desired cleanup. Furthermore, since the get( ) method of a PhantomReference always returns null , such a subclass must also store whatever data is required for the cleanup operation.

Figure 10-88. java.lang.ref.PhantomReference<T>

 public class  PhantomReference<T>  extends Reference<T> {  // Public Constructors  public  PhantomReference  (T  referent  , ReferenceQueue<? super T>  q  );  // Public Methods Overriding Reference  public T  get  ( );  constant  } 



Java In A Nutshell
Java In A Nutshell, 5th Edition
ISBN: 0596007736
EAN: 2147483647
Year: 2004
Pages: 1220

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net