--- old/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java 2015-05-31 14:37:43.345352103 +0300 +++ new/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java 2015-05-31 14:37:42.952344046 +0300 @@ -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,14 @@ } } - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked","rawtypes"}) private Reference reallyPoll() { /* Must hold lock */ Reference r = head; if (r != null) { - head = (r.next == r) ? + Reference rn = r.next; + head = (rn == r) ? null : - r.next; // Unchecked due to the next field having a raw type in Reference + rn; // Unchecked due to the next field having a raw type in Reference r.queue = NULL; r.next = r; queueLength--; @@ -164,4 +167,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. + */ + @SuppressWarnings({"unchecked","rawtypes"}) + void forEach(Consumer> action) { + for (Reference r = head; r != null;) { + action.accept(r); + 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; + } + } + } }