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