Contents | Package | Class | Tree | Deprecated | Index | Help | Java 1.2 Beta 3 | ||
PREV | NEXT | SHOW LISTS | HIDE LISTS |
java.lang.Object | +----java.lang.ref.Reference
The methods defined in this class allow a program to be notified some time after the garbage 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 primitive types of reference objects are supported, each weaker than the last: guarded, weak, and phantom. Each type corresponds to a different level of reachability, as defined below. Guarded references are for implementing sophisticated 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.
Soft references are a fourth type of reference object. Soft references are cleared automatically as the amount of memory available to the Java heap decreases. Because they are implemented in terms of guarded and weak references, soft references are not considered to be primitive.
Each reference-object type is implemented by a 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. Reference objects are otherwise immutable, so there is no
modification operation. 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 a 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 map that uses weak references to implement weak keys could poll
its reference queue each time the map is accessed. Because the
poll
method simply checks an internal data structure, this
check will add little overhead to the map's access methods.
Constructor Summary | |
Reference(Object referent)
|
|
Reference(Object referent,
ReferenceQueue queue)
|
Method Summary | |
void | clear()
|
boolean | enqueue()
|
Object | get()
|
boolean | isEnqueued()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
protected Reference(Object referent)
protected Reference(Object referent, ReferenceQueue queue)
queue
argument
is null
Method Detail |
public Object get()
null
.public void clear()
get
method will return null
.public boolean isEnqueued()
false
.public boolean enqueue()
true
if this was done successfully; return
false
if the reference had already been enqueued, or if it
was not registered with a queue.Contents | Package | Class | Tree | Deprecated | Index | Help | Java 1.2 Beta 3 | ||
PREV | NEXT | SHOW LISTS | HIDE LISTS |