|
Java Platform 1.2 Beta 4 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.lang.ref.Reference
Reference
class contains the core abstractions for
reference objects. Reference objects reify references in much the
same way that Class objects reify Java classes. That is, a reference object
encapsulates a reference to some other object so that the reference itself
may be examined and manipulated like any other object.
The methods defined in this class allow a program to be notified some time after the collector detects a change in the reachability of a given object. They also allow a program to maintain a reference to an object that does not prevent the object from being considered for reclamation by the garbage collector.
Three types of reference objects are supported, each weaker than the last: soft, weak, and phantom. Each type corresponds to a different level of reachability, as defined below. Soft references are for implementing caches, weak references are for implementing mappings that do not prevent their keys (or values) from being reclaimed, and phantom references are for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.
Each reference-object type is implemented by subclass of the
Reference
class. An instance of one of these subclasses
encapsulates a single reference to a particular object, called the
referent. Each subclass provides methods for getting and clearing
the reference. Aside from the clearing operation Reference objects are
otherwise immutable, so no set operation is provided. A program may further
subclass these subclasses, adding whatever fields and methods are required
for its purposes, or it may use these subclasses without change.
ReferenceQueue
class.
The relationship between a registered reference object and its queue is one-sided. That is, a queue does not keep track of the references that are registered with it. If a registered reference becomes unreachable itself, then it will never be enqueued. It is the responsibility of the program using reference objects to ensure that the objects remain reachable for as long as the program is interested in their referents.
While some programs will choose to dedicate a thread to removing
reference objects from one or more queues and processing them, this is by no
means necessary. A tactic that often works well is to examine a reference
queue in the course of performing some other fairly-frequent action. For
example, a hashtable that uses weak references to implement weak keys could
poll its reference queue each time the table is accessed. Because the
poll
method simply checks an internal data structure, this
check will add little overhead to the hashtable access methods.
ReferenceQueue
,
SoftReference
,
WeakReference
,
PhantomReference
Method Summary | |
void | clear()
Clear this reference object. |
boolean | enqueue()
Add this reference to the queue with which it was registered. |
Object | get()
Return the object to which this reference object refers. |
boolean | isEnqueued()
Tell whether or not this reference object has been enqueued. |
Methods inherited from class java.lang.Object | |
clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait |
Method Detail |
public Object get()
null
.public void clear()
public boolean isEnqueued()
false
.public boolean enqueue()
true
if this was done successfully; return
false
if the reference has already been enqueued, or if it
was not registered with a queue when it was created.
|
Java Platform 1.2 Beta 4 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |