Class Reference<T>

java.lang.Object
java.lang.ref.Reference<T>
Type Parameters:
T - the type of the referent
Direct Known Subclasses:
PhantomReference, SoftReference, WeakReference

public abstract sealed class Reference<T> extends Object permits PhantomReference<T>, SoftReference<T>, WeakReference<T> (not exhaustive)
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 Details

    • 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 returns null.
      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)
      Tests if the referent of this reference object is 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 if obj is the referent of this reference object
      Since:
      16
    • clear

      public void clear()
      Clears this reference object. Invoking this method does not enqueue this object, and the garbage collector will not clear or enqueue this object.

      When the garbage collector or the enqueue() method clear references they do so directly, without invoking this method.

    • isEnqueued

      @Deprecated(since="16") public 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. This method could be misused due to the inherent race condition or without an associated 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.
      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:
      • 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.
      Otherwise, this method returns 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()
      Clears this reference object, then attempts to add it to the queue with which it is registered, if any.

      If this reference is registered with a queue but not yet enqueued, the reference is added to the queue; this method is successful and returns true. If this reference is not registered with a queue, or was already enqueued (by the garbage collector, or a previous call to enqueue), this method is unsuccessful and returns false.

      Memory consistency effects: Actions in a thread prior to a successful call to enqueue happen-before the reference is removed from the queue by ReferenceQueue.poll() or ReferenceQueue.remove(long). Unsuccessful calls to enqueue have no specified memory consistency effects.

      When this method clears references it does so directly, without invoking the clear() method. When the garbage collector clears and enqueues references it does so directly, without invoking the clear() method or 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
      Throws CloneNotSupportedException. A Reference cannot be meaningfully cloned. Construct a new Reference instead.
      Overrides:
      clone in class Object
      Returns:
      never returns normally
      Throws:
      CloneNotSupportedException - always
      See Also:
    • reachabilityFence

      public static void reachabilityFence(Object ref)
      Ensures that the given object remains strongly reachable. This reachability is assured regardless of any optimizing transformations the virtual machine may perform that might otherwise allow the object to become unreachable (see JLS ). Thus, the given object is not reclaimable by garbage collection at least until after the invocation of this method. References to the given object will not be cleared (or enqueued, if applicable) by the garbage collector until after invocation of this method. Invocation of this method does not itself initiate reference processing, 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 applicable only when reclamation may have visible effects, such as for objects that use finalizers or Cleaner, or code that performs reference processing.

      Memory consistency effects: Actions in a thread prior to calling reachabilityFence(x) happen-before the garbage collector clears any reference to x.

      Parameters:
      ref - the reference to the object to keep strongly reachable. If null, this method has no effect.
      Since:
      9