< prev index next >

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

Print this page




 124 
 125         static {
 126             // pre-load and initialize Cleaner class so that we don't
 127             // get into trouble later in the run loop if there's
 128             // memory shortage while loading/initializing it lazily.
 129             ensureClassInitialized(Cleaner.class);
 130         }
 131 
 132         ReferenceHandler(ThreadGroup g, String name) {
 133             super(g, null, name, 0, false);
 134         }
 135 
 136         public void run() {
 137             while (true) {
 138                 processPendingReferences();
 139             }
 140         }
 141     }
 142 
 143     /*
 144      * system property to disable clearing before enqueuing.
 145      */
 146     private static final class ClearBeforeEnqueue {
 147         static final boolean DISABLE =
 148             Boolean.getBoolean("jdk.lang.ref.disableClearBeforeEnqueue");
 149     }
 150 
 151     /*
 152      * Atomically get and clear (set to null) the VM's pending list.
 153      */
 154     private static native Reference<Object> getAndClearReferencePendingList();
 155 
 156     /*
 157      * Test whether the VM's pending list contains any entries.
 158      */
 159     private static native boolean hasReferencePendingList();
 160 
 161     /*
 162      * Wait until the VM's pending list may be non-null.
 163      */
 164     private static native void waitForReferencePendingList();
 165 
 166     private static final Object processPendingLock = new Object();
 167     private static boolean processPendingActive = false;
 168 
 169     private static void processPendingReferences() {
 170         // Only the singleton reference processing thread calls
 171         // waitForReferencePendingList() and getAndClearReferencePendingList().


 282      *
 283      * @return   <code>true</code> if and only if this reference object has
 284      *           been enqueued
 285      */
 286     public boolean isEnqueued() {
 287         return (this.queue == ReferenceQueue.ENQUEUED);
 288     }
 289 
 290     /**
 291      * Clears this reference object and adds it to the queue with which
 292      * it is registered, if any.
 293      *
 294      * <p> This method is invoked only by Java code; when the garbage collector
 295      * enqueues references it does so directly, without invoking this method.
 296      *
 297      * @return   <code>true</code> if this reference object was successfully
 298      *           enqueued; <code>false</code> if it was already enqueued or if
 299      *           it was not registered with a queue when it was created
 300      */
 301     public boolean enqueue() {
 302         if (!ClearBeforeEnqueue.DISABLE)
 303             this.referent = null;
 304         return this.queue.enqueue(this);
 305     }
 306 
 307     /* -- Constructors -- */
 308 
 309     Reference(T referent) {
 310         this(referent, null);
 311     }
 312 
 313     Reference(T referent, ReferenceQueue<? super T> queue) {
 314         this.referent = referent;
 315         this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
 316     }
 317 
 318     /**
 319      * Ensures that the object referenced by the given reference remains
 320      * <a href="package-summary.html#reachability"><em>strongly reachable</em></a>,
 321      * regardless of any prior actions of the program that might otherwise cause
 322      * the object to become unreachable; thus, the referenced object is not




 124 
 125         static {
 126             // pre-load and initialize Cleaner class so that we don't
 127             // get into trouble later in the run loop if there's
 128             // memory shortage while loading/initializing it lazily.
 129             ensureClassInitialized(Cleaner.class);
 130         }
 131 
 132         ReferenceHandler(ThreadGroup g, String name) {
 133             super(g, null, name, 0, false);
 134         }
 135 
 136         public void run() {
 137             while (true) {
 138                 processPendingReferences();
 139             }
 140         }
 141     }
 142 
 143     /*








 144      * Atomically get and clear (set to null) the VM's pending list.
 145      */
 146     private static native Reference<Object> getAndClearReferencePendingList();
 147 
 148     /*
 149      * Test whether the VM's pending list contains any entries.
 150      */
 151     private static native boolean hasReferencePendingList();
 152 
 153     /*
 154      * Wait until the VM's pending list may be non-null.
 155      */
 156     private static native void waitForReferencePendingList();
 157 
 158     private static final Object processPendingLock = new Object();
 159     private static boolean processPendingActive = false;
 160 
 161     private static void processPendingReferences() {
 162         // Only the singleton reference processing thread calls
 163         // waitForReferencePendingList() and getAndClearReferencePendingList().


 274      *
 275      * @return   <code>true</code> if and only if this reference object has
 276      *           been enqueued
 277      */
 278     public boolean isEnqueued() {
 279         return (this.queue == ReferenceQueue.ENQUEUED);
 280     }
 281 
 282     /**
 283      * Clears this reference object and adds it to the queue with which
 284      * it is registered, if any.
 285      *
 286      * <p> This method is invoked only by Java code; when the garbage collector
 287      * enqueues references it does so directly, without invoking this method.
 288      *
 289      * @return   <code>true</code> if this reference object was successfully
 290      *           enqueued; <code>false</code> if it was already enqueued or if
 291      *           it was not registered with a queue when it was created
 292      */
 293     public boolean enqueue() {

 294         this.referent = null;
 295         return this.queue.enqueue(this);
 296     }
 297 
 298     /* -- Constructors -- */
 299 
 300     Reference(T referent) {
 301         this(referent, null);
 302     }
 303 
 304     Reference(T referent, ReferenceQueue<? super T> queue) {
 305         this.referent = referent;
 306         this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
 307     }
 308 
 309     /**
 310      * Ensures that the object referenced by the given reference remains
 311      * <a href="package-summary.html#reachability"><em>strongly reachable</em></a>,
 312      * regardless of any prior actions of the program that might otherwise cause
 313      * the object to become unreachable; thus, the referenced object is not


< prev index next >