< prev index next >

src/java.base/share/classes/java/lang/ref/ReferenceQueue.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.ref;
  27 



  28 /**
  29  * Reference queues, to which registered reference objects are appended by the
  30  * garbage collector after the appropriate reachability changes are detected.
  31  *
  32  * @author   Mark Reinhold
  33  * @since    1.2
  34  */
  35 
  36 public class ReferenceQueue<T> {
  37 
  38     /**
  39      * Constructs a new reference-object queue.
  40      */
  41     public ReferenceQueue() { }
  42 
  43     private static class Null<S> extends ReferenceQueue<S> {
  44         boolean enqueue(Reference<? extends S> r) {
  45             return false;
  46         }
  47     }


  66             r.queue = ENQUEUED;
  67             r.next = (head == null) ? r : head;
  68             head = r;
  69             queueLength++;
  70             if (r instanceof FinalReference) {
  71                 sun.misc.VM.addFinalRefCount(1);
  72             }
  73             lock.notifyAll();
  74             return true;
  75         }
  76     }
  77 
  78     @SuppressWarnings("unchecked")
  79     private Reference<? extends T> reallyPoll() {       /* Must hold lock */
  80         Reference<? extends T> r = head;
  81         if (r != null) {
  82             head = (r.next == r) ?
  83                 null :
  84                 r.next; // Unchecked due to the next field having a raw type in Reference
  85             r.queue = NULL;




  86             r.next = r;
  87             queueLength--;
  88             if (r instanceof FinalReference) {
  89                 sun.misc.VM.addFinalRefCount(-1);
  90             }
  91             return r;
  92         }
  93         return null;
  94     }
  95 
  96     /**
  97      * Polls this queue to see if a reference object is available.  If one is
  98      * available without further delay then it is removed from the queue and
  99      * returned.  Otherwise this method immediately returns <tt>null</tt>.
 100      *
 101      * @return  A reference object, if one was immediately available,
 102      *          otherwise <code>null</code>
 103      */
 104     public Reference<? extends T> poll() {
 105         if (head == null)


 147                     long end = System.nanoTime();
 148                     timeout -= (end - start) / 1000_000;
 149                     if (timeout <= 0) return null;
 150                     start = end;
 151                 }
 152             }
 153         }
 154     }
 155 
 156     /**
 157      * Removes the next reference object in this queue, blocking until one
 158      * becomes available.
 159      *
 160      * @return A reference object, blocking until one becomes available
 161      * @throws  InterruptedException  If the wait is interrupted
 162      */
 163     public Reference<? extends T> remove() throws InterruptedException {
 164         return remove(0);
 165     }
 166 


































 167 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.ref;
  27 
  28 import sun.misc.Unsafe;
  29 import java.util.function.Consumer;
  30 
  31 /**
  32  * Reference queues, to which registered reference objects are appended by the
  33  * garbage collector after the appropriate reachability changes are detected.
  34  *
  35  * @author   Mark Reinhold
  36  * @since    1.2
  37  */
  38 
  39 public class ReferenceQueue<T> {
  40 
  41     /**
  42      * Constructs a new reference-object queue.
  43      */
  44     public ReferenceQueue() { }
  45 
  46     private static class Null<S> extends ReferenceQueue<S> {
  47         boolean enqueue(Reference<? extends S> r) {
  48             return false;
  49         }
  50     }


  69             r.queue = ENQUEUED;
  70             r.next = (head == null) ? r : head;
  71             head = r;
  72             queueLength++;
  73             if (r instanceof FinalReference) {
  74                 sun.misc.VM.addFinalRefCount(1);
  75             }
  76             lock.notifyAll();
  77             return true;
  78         }
  79     }
  80 
  81     @SuppressWarnings("unchecked")
  82     private Reference<? extends T> reallyPoll() {       /* Must hold lock */
  83         Reference<? extends T> r = head;
  84         if (r != null) {
  85             head = (r.next == r) ?
  86                 null :
  87                 r.next; // Unchecked due to the next field having a raw type in Reference
  88             r.queue = NULL;
  89             // make sure the store to r.next below doesn't float up
  90             // before the store to r.queue above because we need this
  91             // for correct lock-less iteration in forEach()
  92             U.storeFence();
  93             r.next = r;
  94             queueLength--;
  95             if (r instanceof FinalReference) {
  96                 sun.misc.VM.addFinalRefCount(-1);
  97             }
  98             return r;
  99         }
 100         return null;
 101     }
 102 
 103     /**
 104      * Polls this queue to see if a reference object is available.  If one is
 105      * available without further delay then it is removed from the queue and
 106      * returned.  Otherwise this method immediately returns <tt>null</tt>.
 107      *
 108      * @return  A reference object, if one was immediately available,
 109      *          otherwise <code>null</code>
 110      */
 111     public Reference<? extends T> poll() {
 112         if (head == null)


 154                     long end = System.nanoTime();
 155                     timeout -= (end - start) / 1000_000;
 156                     if (timeout <= 0) return null;
 157                     start = end;
 158                 }
 159             }
 160         }
 161     }
 162 
 163     /**
 164      * Removes the next reference object in this queue, blocking until one
 165      * becomes available.
 166      *
 167      * @return A reference object, blocking until one becomes available
 168      * @throws  InterruptedException  If the wait is interrupted
 169      */
 170     public Reference<? extends T> remove() throws InterruptedException {
 171         return remove(0);
 172     }
 173 
 174     /**
 175      * Iterate queue and invoke given action with each Reference.
 176      * Suitable for diagnostic purposes.
 177      * WARNING: any use of this method should make sure to not
 178      * retain the referents of iterated references (in case of
 179      * FinalReference(s)) so that their life is not prolonged more
 180      * than necessary.
 181      */
 182     void forEach(Consumer<? super Reference<? extends T>> action) {
 183         for (Reference<? extends T> r = head; r != null;) {
 184             action.accept(r);
 185             Reference rn = r.next;
 186             // make sure load of r.queue below doesn't float up before
 187             // the load of r.next above. The corresponding stores are
 188             // performed in reallyPoll() in reverse order.
 189             U.loadFence();
 190             if (rn == r) {
 191                 if (r.queue == ENQUEUED) {
 192                     // still enqueued -> we reached end of chain
 193                     r = null;
 194                 } else {
 195                     // already dequeued: r.queue == NULL; ->
 196                     // restart from head when overtaken by queue poller(s)
 197                     r = head;
 198                 }
 199             } else {
 200                 // next in chain
 201                 r = rn;
 202             }
 203         }
 204     }
 205 
 206     /* we need a fence */
 207     private static final Unsafe U = Unsafe.getUnsafe();
 208 }
< prev index next >