< 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.

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



542         this.referent = referent;
543         this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
544     }
545 
546     /**
547      * Ensures that the given object remains
548      * <a href="package-summary.html#reachability"><em>strongly reachable</em></a>.
549      * This reachability is assured regardless of any optimizing transformations
550      * the virtual machine may perform that might otherwise allow the object to
551      * become unreachable (see JLS {@jls 12.6.1}). Thus, the given object is not
552      * reclaimable by garbage collection at least until after the invocation of
553      * this method. References to the given object will not be cleared (or
554      * enqueued, if applicable) by the garbage collector until after invocation
555      * of this method.
556      * Invocation of this method does not itself initiate reference processing,
557      * garbage collection, or finalization.
558      *
559      * <p> This method establishes an ordering for <em>strong reachability</em>
560      * with respect to garbage collection.  It controls relations that are
561      * 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  *
 43  * <div class="preview-block">
 44  *      <div class="preview-comment">
 45  *          The referent must have {@linkplain Objects#hasIdentity(Object) object identity}.
 46  *          When preview features are enabled, attempts to create a reference
 47  *          to a {@linkplain Class#isValue value object} result in an {@link IdentityException}.
 48  *      </div>
 49  * </div>
 50  * @param <T> the type of the referent
 51  *
 52  * @author   Mark Reinhold
 53  * @since    1.2
 54  * @sealedGraph
 55  */
 56 
 57 public abstract sealed class Reference<T>
 58     permits PhantomReference, SoftReference, WeakReference, FinalReference {
 59 
 60     /* The state of a Reference object is characterized by two attributes.  It
 61      * may be either "active", "pending", or "inactive".  It may also be
 62      * either "registered", "enqueued", "dequeued", or "unregistered".
 63      *
 64      *   Active: Subject to special treatment by the garbage collector.  Some
 65      *   time after the collector detects that the reachability of the
 66      *   referent has changed to the appropriate state, the collector
 67      *   "notifies" the reference, changing the state to either "pending" or
 68      *   "inactive".
 69      *   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 >