--- old/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java 2015-06-03 21:12:20.881758506 +0200 +++ new/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java 2015-06-03 21:12:20.769760437 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -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. @@ -75,13 +77,12 @@ } } - @SuppressWarnings("unchecked") private Reference reallyPoll() { /* Must hold lock */ Reference r = head; if (r != null) { - head = (r.next == r) ? - null : - r.next; // Unchecked due to the next field having a raw type in Reference + @SuppressWarnings("unchecked") + Reference rn = r.next; + head = (rn == r) ? null : rn; r.queue = NULL; r.next = r; queueLength--; @@ -164,4 +165,32 @@ return remove(0); } + /** + * Iterate queue and invoke given action with each Reference. + * Suitable for diagnostic purposes. + * WARNING: any use of this method should make sure to not + * retain the referents of iterated references (in case of + * FinalReference(s)) so that their life is not prolonged more + * than necessary. + */ + void forEach(Consumer> action) { + for (Reference r = head; r != null;) { + action.accept(r); + @SuppressWarnings("unchecked") + Reference rn = r.next; + if (rn == r) { + if (r.queue == ENQUEUED) { + // still enqueued -> we reached end of chain + r = null; + } else { + // already dequeued: r.queue == NULL; -> + // restart from head when overtaken by queue poller(s) + r = head; + } + } else { + // next in chain + r = rn; + } + } + } }