src/share/classes/java/lang/ref/ReferenceQueue.java

Print this page

        

@@ -56,10 +56,15 @@
 
     boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
         synchronized (r) {
             if (r.queue == ENQUEUED) return false;
             synchronized (lock) {
+                // Check that since getting the lock this reference hasn't already been
+                // enqueued (and even then removed)
+                if (r.queue != this) {
+                    return false;
+                }
                 r.queue = ENQUEUED;
                 r.next = (head == null) ? r : head;
                 head = r;
                 queueLength++;
                 if (r instanceof FinalReference) {