< prev index next >

test/java/lang/ref/ReferenceEnqueuePending.java

Print this page
rev 13558 : [mq]: fix_test
rev 13559 : [mq]: inc1
   1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  77             // Create a new object, dropping the onlY strong reference to
  78             // the previous Integer object.
  79             obj = new Integer(i);
  80             // Trigger gc each gc_trigger iterations.
  81             if ((i % gc_trigger) == 0) {
  82                 forceGc(0);
  83             }
  84             // Enqueue every other weaky.
  85             if ((i % 2) == 0) {
  86                 weaky.enqueue();
  87             }
  88             // Remember the Reference objects, for testing later.
  89             b[i - 1] = weaky;
  90             // Get a new weaky for the Integer object just
  91             // created, which may be explicitly enqueued in
  92             // our next trip around the loop.
  93             weaky = new NumberedWeakReference(obj, refQueue, i);
  94         }
  95 
  96         // Do a final collection to discover and process all
  97         // Reference objects created above, allowing enough time
  98         // for the ReferenceHandler thread to queue the References.
  99         forceGc(100);
 100         forceGc(100);
 101 
 102         // Verify that all WeakReference objects ended up queued.
 103         checkResult(refQueue, obj, iterations-1);








 104         System.out.println("Test passed.");
 105     }
 106 








 107     private static void checkResult(ReferenceQueue<Integer> queue,
 108                                     Integer obj,
 109                                     int expected) {
 110         if (debug) {
 111             System.out.println("Reading the queue");
 112         }
 113 
 114         // Empty the queue and record numbers into a[];
 115         NumberedWeakReference weakRead = (NumberedWeakReference) queue.poll();
 116         int length = 0;
 117         while (weakRead != null) {
 118             a[length++] = weakRead.number;



 119             weakRead = (NumberedWeakReference) queue.poll();
 120         }

 121         if (debug) {
 122             System.out.println("Reference Queue had " + length + " elements");
 123         }
 124         // Use the last Reference object of those created above, so as to keep it "alive".
 125         System.out.println("I must write " + obj + " to prevent compiler optimizations.");
 126 
 127 
 128         // verify the queued references: all but the last Reference object
 129         // should have been in the queue.
 130         if (debug) {
 131             System.out.println("Start of final check");
 132         }
 133 
 134         // Sort the first "length" elements in array "a[]".
 135         sort(length);
 136 
 137         boolean fail = (length != expected);
 138         for (int i = 0; i < length; i++) {
 139             if (a[i] != i) {
 140                 if (debug) {
 141                     System.out.println("a[" + i + "] is not " + i + " but " + a[i]);
 142                 }
 143                 fail = true;
 144             }
 145         }


   1 /*
   2  * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  77             // Create a new object, dropping the onlY strong reference to
  78             // the previous Integer object.
  79             obj = new Integer(i);
  80             // Trigger gc each gc_trigger iterations.
  81             if ((i % gc_trigger) == 0) {
  82                 forceGc(0);
  83             }
  84             // Enqueue every other weaky.
  85             if ((i % 2) == 0) {
  86                 weaky.enqueue();
  87             }
  88             // Remember the Reference objects, for testing later.
  89             b[i - 1] = weaky;
  90             // Get a new weaky for the Integer object just
  91             // created, which may be explicitly enqueued in
  92             // our next trip around the loop.
  93             weaky = new NumberedWeakReference(obj, refQueue, i);
  94         }
  95 
  96         // Do a final collection to discover and process all
  97         // Reference objects created above, allowing some time
  98         // for the ReferenceHandler thread to queue the References.
  99         forceGc(100);
 100         forceGc(100);
 101 
 102         // Verify that all WeakReference objects ended up queued.
 103         checkResult(refQueue, iterations-1);
 104 
 105         // Ensure the final weaky is live but won't be enqueued during
 106         // result checking, by ensuring its referent remains live.
 107         // This eliminates behavior changes resulting from different
 108         // compiler optimizations.
 109         Reference.reachabilityFence(weaky);
 110         Reference.reachabilityFence(obj);
 111 
 112         System.out.println("Test passed.");
 113     }
 114 
 115     private static NumberedWeakReference waitForReference(ReferenceQueue<Integer> queue) {
 116         try {
 117             return (NumberedWeakReference) queue.remove(30000); // 30sec
 118         } catch (InterruptedException ie) {
 119             return null;
 120         }
 121     }
 122 
 123     private static void checkResult(ReferenceQueue<Integer> queue,

 124                                     int expected) {
 125         if (debug) {
 126             System.out.println("Reading the queue");
 127         }
 128 
 129         // Empty the queue and record numbers into a[];
 130         NumberedWeakReference weakRead = waitForReference(queue);
 131         int length = 0;
 132         while (weakRead != null) {
 133             a[length++] = weakRead.number;
 134             if (length < expected) {
 135                 weakRead = waitForReference(queue);
 136             } else {            // Check for unexpected extra entries.
 137                 weakRead = (NumberedWeakReference) queue.poll();
 138             }
 139         }
 140         if (debug) {
 141             System.out.println("Reference Queue had " + length + " elements");
 142         }


 143 
 144 
 145         // verify the queued references: all but the last Reference object
 146         // should have been in the queue.
 147         if (debug) {
 148             System.out.println("Start of final check");
 149         }
 150 
 151         // Sort the first "length" elements in array "a[]".
 152         sort(length);
 153 
 154         boolean fail = (length != expected);
 155         for (int i = 0; i < length; i++) {
 156             if (a[i] != i) {
 157                 if (debug) {
 158                     System.out.println("a[" + i + "] is not " + i + " but " + a[i]);
 159                 }
 160                 fail = true;
 161             }
 162         }


< prev index next >