< prev index next >

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

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -138,19 +138,28 @@
                 processPendingReferences();
             }
         }
     }
 
-    /* Atomically get and clear (set to null) the VM's pending list.
+    /*
+     * system property to disable clearing before enqueuing.
+     */
+    private static final boolean disableClearBeforeEnqueue
+        = Boolean.getBoolean("jdk.ref.disableClearBeforeEnqueue");
+
+    /*
+     * Atomically get and clear (set to null) the VM's pending list.
      */
     private static native Reference<Object> getAndClearReferencePendingList();
 
-    /* Test whether the VM's pending list contains any entries.
+    /*
+     * Test whether the VM's pending list contains any entries.
      */
     private static native boolean hasReferencePendingList();
 
-    /* Wait until the VM's pending list may be non-null.
+    /*
+     * Wait until the VM's pending list may be non-null.
      */
     private static native void waitForReferencePendingList();
 
     private static final Object processPendingLock = new Object();
     private static boolean processPendingActive = false;

@@ -259,11 +268,10 @@
      */
     public void clear() {
         this.referent = null;
     }
 
-
     /* -- Queue operations -- */
 
     /**
      * Tells whether or not this reference object has been enqueued, either by
      * the program or by the garbage collector.  If this reference object was

@@ -276,25 +284,26 @@
     public boolean isEnqueued() {
         return (this.queue == ReferenceQueue.ENQUEUED);
     }
 
     /**
-     * Adds this reference object to the queue with which it is registered,
-     * if any.
+     * Clears this reference object and adds it to the queue with which
+     * it is registered, if any.
      *
      * <p> This method is invoked only by Java code; when the garbage collector
      * enqueues references it does so directly, without invoking this method.
      *
      * @return   <code>true</code> if this reference object was successfully
      *           enqueued; <code>false</code> if it was already enqueued or if
      *           it was not registered with a queue when it was created
      */
     public boolean enqueue() {
+        if (!disableClearBeforeEnqueue)
+            this.referent = null;
         return this.queue.enqueue(this);
     }
 
-
     /* -- Constructors -- */
 
     Reference(T referent) {
         this(referent, null);
     }

@@ -417,7 +426,6 @@
     public static void reachabilityFence(Object ref) {
         // Does nothing, because this method is annotated with @DontInline
         // HotSpot needs to retain the ref and not GC it before a call to this
         // method
     }
-
 }
< prev index next >