< prev index next >
src/java.base/share/classes/java/lang/ref/ReferenceQueue.java
Print this page
*** 23,32 ****
--- 23,34 ----
* questions.
*/
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.
*
* @author Mark Reinhold
*** 81,91 ****
if (r != null) {
head = (r.next == r) ?
null :
r.next; // Unchecked due to the next field having a raw type in Reference
r.queue = NULL;
! r.next = r;
queueLength--;
if (r instanceof FinalReference) {
sun.misc.VM.addFinalRefCount(-1);
}
return r;
--- 83,93 ----
if (r != null) {
head = (r.next == r) ?
null :
r.next; // Unchecked due to the next field having a raw type in Reference
r.queue = NULL;
! r.next = Reference.DEQUEUED;
queueLength--;
if (r instanceof FinalReference) {
sun.misc.VM.addFinalRefCount(-1);
}
return r;
*** 162,167 ****
--- 164,188 ----
*/
public Reference<? extends T> remove() throws InterruptedException {
return remove(0);
}
+ /**
+ * Iterate queue and invoke given action with each Reference.
+ * Suitable for diagnostic purposes.
+ */
+ void forEach(Consumer<? super Reference<? extends T>> action) {
+ for (Reference<? extends T> 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;
+ }
+ }
+ }
}
< prev index next >