-
Bug
-
Resolution: Duplicate
-
P3
-
hs25, 8
-
generic
-
solaris
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8020452 | 8 | Mandy Chung | P3 | Closed | Fixed | b100 |
The following program fails because the reference queue returns more references than actually enqueued.
import java.lang.ref.*;
public class EnqueuePollRace {
public static void main(String args[]) throws Exception {
new WeakRef().run();
System.out.println("Test passed.");
}
static class WeakRef {
ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
final int numReferences = 100;
final Reference refs[] = new Reference[numReferences];
final int iterations = 100;
void run() throws InterruptedException {
for (int i = 0; i < iterations; i++) {
queue = new ReferenceQueue<Object>();
for (int j = 0; j < refs.length; j++) {
refs[j] = new WeakReference(new Object(), queue);
}
System.gc(); // enqueues references into the list of discovered references
// now manually enqueue all of them
for (int j = 0; j < refs.length; j++) {
refs[j].enqueue();
}
// and get them back. There should be exactly numReferences
// entries in the queue now.
int foundReferences = 0;
while (queue.poll() != null) {
foundReferences++;
}
if (foundReferences != refs.length) {
throw new RuntimeException("Got " + foundReferences + " references in the queue, but expected " + refs.length);
}
}
}
}
}
import java.lang.ref.*;
public class EnqueuePollRace {
public static void main(String args[]) throws Exception {
new WeakRef().run();
System.out.println("Test passed.");
}
static class WeakRef {
ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
final int numReferences = 100;
final Reference refs[] = new Reference[numReferences];
final int iterations = 100;
void run() throws InterruptedException {
for (int i = 0; i < iterations; i++) {
queue = new ReferenceQueue<Object>();
for (int j = 0; j < refs.length; j++) {
refs[j] = new WeakReference(new Object(), queue);
}
System.gc(); // enqueues references into the list of discovered references
// now manually enqueue all of them
for (int j = 0; j < refs.length; j++) {
refs[j].enqueue();
}
// and get them back. There should be exactly numReferences
// entries in the queue now.
int foundReferences = 0;
while (queue.poll() != null) {
foundReferences++;
}
if (foundReferences != refs.length) {
throw new RuntimeException("Got " + foundReferences + " references in the queue, but expected " + refs.length);
}
}
}
}
}
- backported by
-
JDK-8020452 (ref) Reference queues may return more entries than expected
- Closed
- relates to
-
JDK-4243978 (ref) Race condition in Reference.enqueue()
- Closed