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