T
- the type of the referent
PhantomReference
, SoftReference
, WeakReference
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.
void
Clears this reference object.
boolean
Clears this reference object and adds it to the queue with which it is registered, if any.
Returns this reference object's referent.
boolean
static void
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
Tests if the referent of this reference object is obj
.
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 returns null
.
refersTo
method can be used to avoid such strengthening when testing whether some object is the referent of a reference object; that is, use ref.refersTo(obj)
rather than ref.get() == obj
.
null
if this reference object has been cleared
Tests if the referent of this reference object is obj
. Using a null
obj
returns true
if the reference object has been cleared.
obj
- the object to compare with this reference object's referent
true
if obj
is the referent of this reference object
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.
Tests if this reference object is in its associated queue, if any. This method returns
true
only if all of the following conditions are met:
enqueue()
is called; andOtherwise, this method returns
false
. This method may return
false
if this reference object has been cleared but not enqueued due to the race condition.
true
if and only if this reference object is in its associated queue (if any).
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.
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
in class Object
CloneNotSupportedException
- always
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 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.
action
uses a reachabilityFence
to ensure that the Resource
object is not reclaimed before bookkeeping on an associated ExternalResource
has been performed; in particular here, to ensure that the array slot holding the ExternalResource
is not nulled out in method Object.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 = ...;
}
}
Here, the invocation of reachabilityFence
is nonintuitively placed after the call to update
, to ensure that the array slot is not nulled out by Object.finalize()
before the update, even if the call to action
was the last use of this object. This might be the case if, for example a usage in a user program had the form new Resource().action();
which retains no other reference to this Resource
. While probably overkill here, reachabilityFence
is placed in a finally
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 that reachabilityFence
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 method update
to proceed even if the finalizer had already executed (nulling out slot), then you could localize use of reachabilityFence
:
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 class Resource
(including finalize
) were enclosed in synchronized (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, method reachabilityFence
remains a better option in cases where this approach is not as efficient, desirable, or possible; for example because it would encounter deadlock.
ref
- the reference. If null
, this method has no effect.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4