Java Reference
In-Depth Information
17.5.3. Reference Queues
When an object changes reachability state, references to the object may
be placed on a reference queue. These queues are used by the garbage
collector to communicate with your code about reachability changes.
They are usually the best way to detect such changes, although you can
sometimes poll for changes as well, by seeing if get returns null .
Reference objects can be associated with a particular queue when they
are constructed. Each of the Reference subclasses provide a constructor
of the form
public Strength Reference(T referent, ReferenceQueue<? super T> q)
Creates a new reference object with the given referent and
registered with the given queue.
Both weak and soft references are enqueued at some point after the
garbage collector determines that their referent has entered that partic-
ular reachability state, and in both cases they are cleared before being
enqueued. Phantom references are also enqueued at some point after
the garbage collector determines the referent is phantom reachable, but
they are not cleared. Once a reference object has been queued by the
garbage collector, it is guaranteed that get will return null , and so the
object cannot be resurrected.
Registering a reference object with a reference queue does not create a
reference between the queue and the reference object. If your reference
object itself becomes unreachable, then it will never be enqueued. So
your application needs to keep a strong reference to all reference ob-
jects.
The ReferenceQueue class provides three methods for removing referen-
ces from the queue:
public Reference<? extends T> poll()
 
Search WWH ::




Custom Search