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

Print this page

        

@@ -23,10 +23,12 @@
  * questions.
  */
 
 package java.lang.ref;
 
+import static java.util.concurrent.TimeUnit.*;
+
 /**
  * Reference queues, to which registered reference objects are appended by the
  * garbage collector after the appropriate reachability changes are detected.
  *
  * @author   Mark Reinhold

@@ -136,15 +138,21 @@
             throw new IllegalArgumentException("Negative timeout value");
         }
         synchronized (lock) {
             Reference<? extends T> r = reallyPoll();
             if (r != null) return r;
+            long start = (timeout == 0) ? 0 : System.nanoTime();
             for (;;) {
                 lock.wait(timeout);
                 r = reallyPoll();
                 if (r != null) return r;
-                if (timeout != 0) return null;
+                if (timeout != 0) {
+                    long end = System.nanoTime();
+                    timeout -= NANOSECONDS.toMillis(end - start);
+                    if (timeout <= 0) return null;
+                    start = end;
+                }
             }
         }
     }
 
     /**