6666739: (ref) ReferenceQueue.poll() doesn't scale well
authoralanb
Thu, 02 Apr 2009 11:19:34 +0100
changeset 2442 f8bb207f9458
parent 2441 228c040622a2
child 2443 4bcc75ed04c0
6666739: (ref) ReferenceQueue.poll() doesn't scale well 6711667: (ref) Update SoftReference timestamp only if clock advances Summary: Forward port from 6u14; originally fixed by Tom Rodriguez in earlier update Reviewed-by: martin
jdk/src/share/classes/java/lang/ref/ReferenceQueue.java
jdk/src/share/classes/java/lang/ref/SoftReference.java
--- a/jdk/src/share/classes/java/lang/ref/ReferenceQueue.java	Thu Apr 02 11:13:56 2009 +0100
+++ b/jdk/src/share/classes/java/lang/ref/ReferenceQueue.java	Thu Apr 02 11:19:34 2009 +0100
@@ -51,7 +51,7 @@
 
     static private class Lock { };
     private Lock lock = new Lock();
-    private Reference<? extends T> head = null;
+    private volatile Reference<? extends T> head = null;
     private long queueLength = 0;
 
     boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
@@ -95,6 +95,8 @@
      *          otherwise <code>null</code>
      */
     public Reference<? extends T> poll() {
+        if (head == null)
+            return null;
         synchronized (lock) {
             return reallyPoll();
         }
--- a/jdk/src/share/classes/java/lang/ref/SoftReference.java	Thu Apr 02 11:13:56 2009 +0100
+++ b/jdk/src/share/classes/java/lang/ref/SoftReference.java	Thu Apr 02 11:19:34 2009 +0100
@@ -63,11 +63,13 @@
 
 public class SoftReference<T> extends Reference<T> {
 
-    /* Timestamp clock, updated by the garbage collector
+    /**
+     * Timestamp clock, updated by the garbage collector
      */
     static private long clock;
 
-    /* Timestamp updated by each invocation of the get method.  The VM may use
+    /**
+     * Timestamp updated by each invocation of the get method.  The VM may use
      * this field when selecting soft references to be cleared, but it is not
      * required to do so.
      */
@@ -108,7 +110,8 @@
      */
     public T get() {
         T o = super.get();
-        if (o != null) this.timestamp = clock;
+        if (o != null && this.timestamp != clock)
+            this.timestamp = clock;
         return o;
     }