jdk/src/java.base/share/classes/java/lang/ref/Reference.java
changeset 44119 e813e246ef17
parent 40954 72ca7f63532a
child 44748 e7d6b4646d04
--- a/jdk/src/java.base/share/classes/java/lang/ref/Reference.java	Thu Mar 09 14:12:24 2017 +0530
+++ b/jdk/src/java.base/share/classes/java/lang/ref/Reference.java	Thu Mar 09 07:41:48 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -140,15 +140,24 @@
         }
     }
 
-    /* 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.lang.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();
 
@@ -261,7 +270,6 @@
         this.referent = null;
     }
 
-
     /* -- Queue operations -- */
 
     /**
@@ -278,8 +286,8 @@
     }
 
     /**
-     * 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.
@@ -289,10 +297,11 @@
      *           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) {
@@ -419,5 +428,4 @@
         // HotSpot needs to retain the ref and not GC it before a call to this
         // method
     }
-
 }