- java.lang.Object
-
- java.lang.ref.Reference<T>
-
- Direct Known Subclasses:
PhantomReference,SoftReference,WeakReference
public abstract class Reference<T> extends Object
Abstract base class for reference objects. This class defines the operations common to all reference objects. Because reference objects are implemented in close cooperation with the garbage collector, this class may not be subclassed directly.- Since:
- 1.2
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears this reference object.protected Objectclone()ThrowsCloneNotSupportedException.booleanenqueue()Clears this reference object and adds it to the queue with which it is registered, if any.Tget()Returns this reference object's referent.booleanisEnqueued()Tells whether or not this reference object has been enqueued, either by the program or by the garbage collector.static voidreachabilityFence(Object ref)Ensures that the object referenced by the given reference remains strongly reachable, regardless of any prior actions of the program that might otherwise cause the object to become unreachable; thus, the referenced object is not reclaimable by garbage collection at least until after the invocation of this method.
-
-
-
Method Detail
-
get
public T get()
Returns this reference object's referent. If this reference object has been cleared, either by the program or by the garbage collector, then this method returnsnull.- Returns:
- The object to which this reference refers, or
nullif this reference object has been cleared
-
clear
public void clear()
Clears this reference object. Invoking this method will not cause this object to be enqueued.This method is invoked only by Java code; when the garbage collector clears references it does so directly, without invoking this method.
-
isEnqueued
public boolean isEnqueued()
Tells whether or not this reference object has been enqueued, either by the program or by the garbage collector. If this reference object was not registered with a queue when it was created, then this method will always returnfalse.- Returns:
trueif and only if this reference object has been enqueued
-
enqueue
public boolean enqueue()
Clears this reference object and adds it to the queue with which it is registered, if any.This method is invoked only by Java code; when the garbage collector enqueues references it does so directly, without invoking this method.
- Returns:
trueif this reference object was successfully enqueued;falseif it was already enqueued or if it was not registered with a queue when it was created
-
clone
protected Object clone() throws CloneNotSupportedException
ThrowsCloneNotSupportedException. AReferencecannot be meaningfully cloned. Construct a newReferenceinstead.- Overrides:
clonein classObject- Returns:
- a clone of this instance.
- Throws:
CloneNotSupportedException- always- Since:
- 11
- See Also:
Cloneable
-
reachabilityFence
public static void reachabilityFence(Object ref)
Ensures that the object referenced by the given reference remains strongly reachable, regardless of any prior actions of the program that might otherwise cause the object to become unreachable; thus, the referenced object is not reclaimable by garbage collection at least until after the invocation of this method. Invocation of this method does not itself initiate garbage collection or finalization.This method establishes an ordering for strong reachability with respect to garbage collection. It controls relations that are otherwise only implicit in a program -- the reachability conditions triggering garbage collection. This method is designed for use in uncommon situations of premature finalization where using
synchronizedblocks or methods, or using other synchronization facilities are not possible or do not provide the desired control. This method is applicable only when reclamation may have visible effects, which is possible for objects with finalizers (See Section 12.6 17 of The Java™ Language Specification) that are implemented in ways that rely on ordering control for correctness.- Parameters:
ref- the reference. Ifnull, this method has no effect.- Since:
- 9
-
-