Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8014890

(ref) Reference queues may return more entries than expected

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      solaris

      Backports

        Description

        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);
                        }
                    }
                }
            }
        }

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                tschatzl Thomas Schatzl
                Reporter:
                dfazunen Dmitry Fazunenko (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: