< prev index next >

src/java.base/share/classes/java/lang/ref/Reference.java

Print this page

 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang.ref;
 27 
 28 import jdk.internal.misc.Unsafe;
 29 import jdk.internal.vm.annotation.ForceInline;
 30 import jdk.internal.vm.annotation.IntrinsicCandidate;
 31 import jdk.internal.access.JavaLangRefAccess;
 32 import jdk.internal.access.SharedSecrets;
 33 import jdk.internal.ref.Cleaner;
 34 


 35 /**
 36  * Abstract base class for reference objects.  This class defines the
 37  * operations common to all reference objects.  Because reference objects are
 38  * implemented in close cooperation with the garbage collector, this class may
 39  * not be subclassed directly.



 40  * @param <T> the type of the referent
 41  *
 42  * @author   Mark Reinhold
 43  * @since    1.2
 44  * @sealedGraph
 45  */
 46 
 47 public abstract sealed class Reference<T>
 48     permits PhantomReference, SoftReference, WeakReference, FinalReference {
 49 
 50     /* The state of a Reference object is characterized by two attributes.  It
 51      * may be either "active", "pending", or "inactive".  It may also be
 52      * either "registered", "enqueued", "dequeued", or "unregistered".
 53      *
 54      *   Active: Subject to special treatment by the garbage collector.  Some
 55      *   time after the collector detects that the reachability of the
 56      *   referent has changed to the appropriate state, the collector
 57      *   "notifies" the reference, changing the state to either "pending" or
 58      *   "inactive".
 59      *   referent != null; discovered = null, or in GC discovered list.

527 
528     /**
529      * Throws {@link CloneNotSupportedException}. A {@code Reference} cannot be
530      * meaningfully cloned. Construct a new {@code Reference} instead.
531      *
532      * @return never returns normally
533      * @throws  CloneNotSupportedException always
534      */
535     @Override
536     protected Object clone() throws CloneNotSupportedException {
537         throw new CloneNotSupportedException();
538     }
539 
540     /* -- Constructors -- */
541 
542     Reference(T referent) {
543         this(referent, null);
544     }
545 
546     Reference(T referent, ReferenceQueue<? super T> queue) {



547         this.referent = referent;
548         this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
549     }
550 
551     /**
552      * Ensures that the given object remains
553      * <a href="package-summary.html#reachability"><em>strongly reachable</em></a>.
554      * This reachability is assured regardless of any optimizing transformations
555      * the virtual machine may perform that might otherwise allow the object to
556      * become unreachable (see JLS {@jls 12.6.1}). Thus, the given object is not
557      * reclaimable by garbage collection at least until after the invocation of
558      * this method. References to the given object will not be cleared (or
559      * enqueued, if applicable) by the garbage collector until after invocation
560      * of this method.
561      * Invocation of this method does not itself initiate reference processing,
562      * garbage collection, or finalization.
563      *
564      * <p> This method establishes an ordering for <em>strong reachability</em>
565      * with respect to garbage collection.  It controls relations that are
566      * otherwise only implicit in a program -- the reachability conditions

 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang.ref;
 27 
 28 import jdk.internal.misc.Unsafe;
 29 import jdk.internal.vm.annotation.ForceInline;
 30 import jdk.internal.vm.annotation.IntrinsicCandidate;
 31 import jdk.internal.access.JavaLangRefAccess;
 32 import jdk.internal.access.SharedSecrets;
 33 import jdk.internal.ref.Cleaner;
 34 
 35 import java.util.Objects;
 36 
 37 /**
 38  * Abstract base class for reference objects.  This class defines the
 39  * operations common to all reference objects.  Because reference objects are
 40  * implemented in close cooperation with the garbage collector, this class may
 41  * not be subclassed directly.
 42  * <p>
 43  * The referent must be an {@linkplain Objects#hasIdentity(Object) identity object}.
 44  * Attempts to create a reference to a value object result in an {@link IdentityException}.
 45  * @param <T> the type of the referent
 46  *
 47  * @author   Mark Reinhold
 48  * @since    1.2
 49  * @sealedGraph
 50  */
 51 
 52 public abstract sealed class Reference<T>
 53     permits PhantomReference, SoftReference, WeakReference, FinalReference {
 54 
 55     /* The state of a Reference object is characterized by two attributes.  It
 56      * may be either "active", "pending", or "inactive".  It may also be
 57      * either "registered", "enqueued", "dequeued", or "unregistered".
 58      *
 59      *   Active: Subject to special treatment by the garbage collector.  Some
 60      *   time after the collector detects that the reachability of the
 61      *   referent has changed to the appropriate state, the collector
 62      *   "notifies" the reference, changing the state to either "pending" or
 63      *   "inactive".
 64      *   referent != null; discovered = null, or in GC discovered list.

532 
533     /**
534      * Throws {@link CloneNotSupportedException}. A {@code Reference} cannot be
535      * meaningfully cloned. Construct a new {@code Reference} instead.
536      *
537      * @return never returns normally
538      * @throws  CloneNotSupportedException always
539      */
540     @Override
541     protected Object clone() throws CloneNotSupportedException {
542         throw new CloneNotSupportedException();
543     }
544 
545     /* -- Constructors -- */
546 
547     Reference(T referent) {
548         this(referent, null);
549     }
550 
551     Reference(T referent, ReferenceQueue<? super T> queue) {
552         if (referent != null) {
553             Objects.requireIdentity(referent);
554         }
555         this.referent = referent;
556         this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
557     }
558 
559     /**
560      * Ensures that the given object remains
561      * <a href="package-summary.html#reachability"><em>strongly reachable</em></a>.
562      * This reachability is assured regardless of any optimizing transformations
563      * the virtual machine may perform that might otherwise allow the object to
564      * become unreachable (see JLS {@jls 12.6.1}). Thus, the given object is not
565      * reclaimable by garbage collection at least until after the invocation of
566      * this method. References to the given object will not be cleared (or
567      * enqueued, if applicable) by the garbage collector until after invocation
568      * of this method.
569      * Invocation of this method does not itself initiate reference processing,
570      * garbage collection, or finalization.
571      *
572      * <p> This method establishes an ordering for <em>strong reachability</em>
573      * with respect to garbage collection.  It controls relations that are
574      * otherwise only implicit in a program -- the reachability conditions
< prev index next >