--- old/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java 2015-07-28 16:53:35.267348353 -0400 +++ new/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java 2015-07-28 16:53:35.147347758 -0400 @@ -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 @@ -63,10 +63,17 @@ return false; } assert queue == this; - r.queue = ENQUEUED; r.next = (head == null) ? r : head; head = r; queueLength++; + // Assignment of queue to special ENQUEUED queue must be + // after the reference is added to the queue's list. + // Otherwise, there is a race condition where the + // reference appears to be enqueued (isEnqueue returns + // true, another enqueue returns false), but polling the + // queue could find it still empty. The q.head and + // r.queue fields are volatile, ensuring that order. + r.queue = ENQUEUED; if (r instanceof FinalReference) { sun.misc.VM.addFinalRefCount(1); }