--- old/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java 2015-05-12 23:11:45.180647480 +0200 +++ new/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java 2015-05-12 23:11:45.106648786 +0200 @@ -25,6 +25,8 @@ package java.lang.ref; +import java.util.function.Consumer; + /** * Reference queues, to which registered reference objects are appended by the * garbage collector after the appropriate reachability changes are detected. @@ -83,7 +85,7 @@ null : r.next; // Unchecked due to the next field having a raw type in Reference r.queue = NULL; - r.next = r; + r.next = Reference.DEQUEUED; queueLength--; if (r instanceof FinalReference) { sun.misc.VM.addFinalRefCount(-1); @@ -164,4 +166,23 @@ return remove(0); } + /** + * Iterate queue and invoke given action with each Reference. + * Suitable for diagnostic purposes. + */ + void forEach(Consumer> action) { + for (Reference r = head; r != null;) { + action.accept(r); + Reference rn = r.next; + if (rn == r) { + // end of chain + r = null; + } else if (rn == Reference.DEQUEUED) { + // restart from head if overtaken by queue poller(s) + r = head; + } else { + r = rn; + } + } + } }