--- old/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java 2015-07-30 18:40:39.091492765 -0400 +++ new/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java 2015-07-30 18:40:38.951492071 -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,13 @@ return false; } assert queue == this; - r.queue = ENQUEUED; r.next = (head == null) ? r : head; head = r; queueLength++; + // Update r.queue *after* adding to list, to avoid race + // with concurrent enqueued checks and fast-path poll(). + // Volatiles ensure ordering. + r.queue = ENQUEUED; if (r instanceof FinalReference) { sun.misc.VM.addFinalRefCount(1); } @@ -79,10 +82,13 @@ private Reference reallyPoll() { /* Must hold lock */ Reference r = head; if (r != null) { + r.queue = NULL; + // Update r.queue *before* removing from list, to avoid + // race with concurrent enqueued checks and fast-path + // poll(). Volatiles ensure ordering. 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) {