Source for java.lang.ref.ReferenceQueue

   1: /* java.lang.ref.ReferenceQueue
   2:    Copyright (C) 1999, 2004, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.lang.ref;
  40: 
  41: /**
  42:  * This is the queue, where references can enqueue themselve on.  Each
  43:  * reference may be registered to a queue at initialization time and
  44:  * will be appended to the queue, when the enqueue method is called.
  45:  *
  46:  * The enqueue method may be automatically called by the garbage
  47:  * collector if it detects, that the object is only reachable through
  48:  * the Reference objects.
  49:  *
  50:  * @author Jochen Hoenicke
  51:  * @see Reference#enqueue()
  52:  */
  53: public class ReferenceQueue<T>
  54: {
  55:   /**
  56:    * This is a linked list of references.  If this is null, the list is
  57:    * empty.  Otherwise this points to the first reference on the queue.
  58:    * The first reference will point to the next reference via the
  59:    * <code>nextOnQueue</code> field.  The last reference will point to
  60:    * itself (not to null, since <code>nextOnQueue</code> is used to
  61:    * determine if a reference is enqueued).
  62:    */
  63:   private Reference<? extends T> first;
  64: 
  65:   /**
  66:    * This is the lock that protects our linked list and is used to signal
  67:    * a thread waiting in remove().
  68:    */
  69:   private final Object lock = new Object();
  70: 
  71:   /**
  72:    * Creates a new empty reference queue.
  73:    */
  74:   public ReferenceQueue()
  75:   {
  76:   }
  77: 
  78:   /**
  79:    * Checks if there is a reference on the queue, returning it
  80:    * immediately.  The reference will be dequeued.
  81:    *
  82:    * @return a reference on the queue, if there is one,
  83:    * <code>null</code> otherwise.
  84:    */
  85:   public Reference<? extends T> poll()
  86:   {
  87:     return dequeue();
  88:   }
  89: 
  90:   /**
  91:    * This is called by reference to enqueue itself on this queue.
  92:    * @param ref the reference that should be enqueued.
  93:    * @return true if successful, false if not.
  94:    */
  95:   final boolean enqueue(Reference<? extends T> ref)
  96:     {
  97:     synchronized (lock)
  98:       {
  99:         if (ref.queue != this)
 100:           return false;
 101: 
 102:         /* last reference will point to itself */
 103:         ref.nextOnQueue = first == null ? ref : first;
 104:         ref.queue = null;
 105:         first = ref;
 106:         /* this wakes only one remove thread. */
 107:         lock.notify();
 108:         return true;
 109:       }
 110:   }
 111: 
 112:   /**
 113:    * Remove a reference from the queue, if there is one.
 114:    * @return the first element of the queue, or null if there isn't any.
 115:    */
 116:   private Reference<? extends T> dequeue()
 117:   {
 118:     synchronized (lock)
 119:       {
 120:         if (first == null)
 121:           return null;
 122: 
 123:         Reference<? extends T> result = first;
 124:         first = (first == first.nextOnQueue) ? null : first.nextOnQueue;
 125:         result.nextOnQueue = null;
 126:         return result;
 127:       }
 128:   }
 129: 
 130:   /**
 131:    * Removes a reference from the queue, blocking for <code>timeout</code>
 132:    * until a reference is enqueued.
 133:    * @param timeout the timeout period in milliseconds, <code>0</code> means
 134:    * wait forever.
 135:    * @return the reference removed from the queue, or
 136:    * <code>null</code> if timeout period expired.
 137:    * @exception InterruptedException if the wait was interrupted.
 138:    */
 139:   public Reference<? extends T> remove(long timeout)
 140:     throws InterruptedException
 141:   {
 142:     synchronized (lock)
 143:       {
 144:         if (first == null)
 145:           lock.wait(timeout);
 146:       }
 147: 
 148:     return dequeue();
 149:   }
 150: 
 151: 
 152:   /**
 153:    * Removes a reference from the queue, blocking until a reference is
 154:    * enqueued.
 155:    *
 156:    * @return the reference removed from the queue.
 157:    * @exception InterruptedException if the wait was interrupted.
 158:    */
 159:   public Reference<? extends T> remove()
 160:     throws InterruptedException
 161:   {
 162:     return remove(0L);
 163:   }
 164: }