Class Reference<T>
- Direct Known Subclasses:
-
PhantomReference
,SoftReference
,WeakReference
public abstract class Reference<T> extends Object
- Since:
- 1.2
Method Summary
Modifier and Type | Method | Description |
---|---|---|
void |
clear() |
Clears this reference object. |
protected Object |
clone() |
Throws CloneNotSupportedException . |
boolean |
enqueue() |
Clears this reference object and adds it to the queue with which it is registered, if any. |
T |
get() |
Returns this reference object's referent. |
boolean |
isEnqueued() |
Deprecated. This method was originally specified to test if a reference object has been cleared and enqueued but was never implemented to do this test. |
static void |
reachabilityFence |
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. |
final boolean |
refersTo |
Tests if the referent of this reference object is obj . |
Method Details
get
public T get()
null
.- API Note:
- This method returns a strong reference to the referent. This may cause the garbage collector to treat it as strongly reachable until some later collection cycle. The
refersTo
method can be used to avoid such strengthening when testing whether some object is the referent of a reference object; that is, useref.refersTo(obj)
rather thanref.get() == obj
. - Returns:
- The object to which this reference refers, or
null
if this reference object has been cleared - See Also:
refersTo
public final boolean refersTo(T obj)
obj
. Using a null
obj
returns true
if the reference object has been cleared.- Parameters:
-
obj
- the object to compare with this reference object's referent - Returns:
-
true
ifobj
is the referent of this reference object - Since:
- 16
clear
public void clear()
This method is invoked only by Java code; when the garbage collector clears references it does so directly, without invoking this method.
isEnqueued
@Deprecated(since="16") public boolean isEnqueued()
ReferenceQueue
. An application relying on this method to release critical resources could cause serious performance issue. An application should use ReferenceQueue
to reliably determine what reference objects that have been enqueued or refersTo(null)
to determine if this reference object has been cleared.true
only if all of the following conditions are met: - this reference object was registered with a queue when it was created; and
- the garbage collector has added this reference object to the queue or
enqueue()
is called; and - this reference object is not yet removed from the queue.
false
. This method may return false
if this reference object has been cleared but not enqueued due to the race condition.- Returns:
-
true
if and only if this reference object is in its associated queue (if any).
enqueue
public boolean enqueue()
This method is invoked only by Java code; when the garbage collector enqueues references it does so directly, without invoking this method.
- Returns:
-
true
if this reference object was successfully enqueued;false
if it was already enqueued or if it was not registered with a queue when it was created
clone
protected Object clone() throws CloneNotSupportedException
CloneNotSupportedException
. A Reference
cannot be meaningfully cloned. Construct a new Reference
instead.- Overrides:
-
clone
in classObject
- Returns:
- never returns normally
- Throws:
-
CloneNotSupportedException
- always - Since:
- 11
- See Also:
reachabilityFence
public static void reachabilityFence(Object ref)
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 synchronized
blocks 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 of The Java Language Specification) that are implemented in ways that rely on ordering control for correctness.
- API Note:
- Finalization may occur whenever the virtual machine detects that no reference to an object will ever be stored in the heap: The garbage collector may reclaim an object even if the fields of that object are still in use, so long as the object has otherwise become unreachable. This may have surprising and undesirable effects in cases such as the following example in which the bookkeeping associated with a class is managed through array indices. Here, method
action
uses areachabilityFence
to ensure that theResource
object is not reclaimed before bookkeeping on an associatedExternalResource
has been performed; in particular here, to ensure that the array slot holding theExternalResource
is not nulled out in methodObject.finalize()
, which may otherwise run concurrently.class Resource { private static ExternalResource[] externalResourceArray = ... int myIndex; Resource(...) { myIndex = ... externalResourceArray[myIndex] = ...; ... } protected void finalize() { externalResourceArray[myIndex] = null; ... } public void action() { try { // ... int i = myIndex; Resource.update(externalResourceArray[i]); } finally { Reference.reachabilityFence(this); } } private static void update(ExternalResource ext) { ext.status = ...; } }
reachabilityFence
is nonintuitively placed after the call toupdate
, to ensure that the array slot is not nulled out byObject.finalize()
before the update, even if the call toaction
was the last use of this object. This might be the case if, for example a usage in a user program had the formnew Resource().action();
which retains no other reference to thisResource
. While probably overkill here,reachabilityFence
is placed in afinally
block to ensure that it is invoked across all paths in the method. In a method with more complex control paths, you might need further precautions to ensure thatreachabilityFence
is encountered along all of them.It is sometimes possible to better encapsulate use of
reachabilityFence
. Continuing the above example, if it were acceptable for the call to methodupdate
to proceed even if the finalizer had already executed (nulling out slot), then you could localize use ofreachabilityFence
:public void action2() { // ... Resource.update(getExternalResource()); } private ExternalResource getExternalResource() { ExternalResource ext = externalResourceArray[myIndex]; Reference.reachabilityFence(this); return ext; }
Method
reachabilityFence
is not required in constructions that themselves ensure reachability. For example, because objects that are locked cannot, in general, be reclaimed, it would suffice if all accesses of the object, in all methods of classResource
(includingfinalize
) were enclosed insynchronized (this)
blocks. (Further, such blocks must not include infinite loops, or themselves be unreachable, which fall into the corner case exceptions to the "in general" disclaimer.) However, methodreachabilityFence
remains a better option in cases where this approach is not as efficient, desirable, or possible; for example because it would encounter deadlock. - Parameters:
-
ref
- the reference. Ifnull
, this method has no effect. - See Java Language Specification:
- 12.6 Finalization of Class Instances
- Since:
- 9
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ref/Reference.html