home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / lang / ref / ReferenceQueue.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.3 KB  |  124 lines

  1. /*
  2.  * @(#)ReferenceQueue.java    1.6 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang.ref;
  16.  
  17.  
  18. /**
  19.  * Reference queues, to which registered reference objects are appended by the
  20.  * garbage collector after the appropriate reachability changes are detected.
  21.  *
  22.  * @see java.lang.ref.Reference
  23.  * @since JDK1.2
  24.  */
  25.  
  26. public class ReferenceQueue {
  27.  
  28.     static private class Lock { };
  29.     private Lock pendingLock = new Lock();
  30.     private Reference pending = null;
  31.  
  32.     /**
  33.      * Construct a new reference-object queue.
  34.      */
  35.     public ReferenceQueue() { }
  36.  
  37.     boolean enqueue(Reference r) {    /* Called only by Reference class */
  38.     synchronized (pendingLock) {
  39.         if (r.queue != null) {
  40.         r.nextPending = pending;
  41.         pending = r;
  42.         pendingLock.notifyAll();
  43.         r.queue = null;
  44.         return true;
  45.         } else {
  46.         return false;
  47.         }
  48.     }
  49.     }
  50.  
  51.     private Reference reallyPoll() {    /* Must hold pendingLock */
  52.     if (pending != null) {
  53.         Reference r = pending;
  54.         pending = r.nextPending;
  55.         r.queue = null;
  56.         r.state = ((r instanceof WeakReference)
  57.                ? Reference.ACTIVE : Reference.IDLE);
  58.         r.nextPending = null;
  59.         return r;
  60.     }
  61.     return null;
  62.     }
  63.  
  64.     /**
  65.      * Poll this queue to see if a reference object is available,
  66.      * immediately returning one if so.  If the queue is empty, this
  67.      * method immediately returns <code>null</code>.
  68.      *
  69.      * @return  A reference, if one was immediately available, otherwise
  70.      *          <code>null</code>.
  71.      */
  72.     public Reference poll() {
  73.     synchronized (pendingLock) {
  74.         return reallyPoll();
  75.     }
  76.     }
  77.  
  78.     /**
  79.      * Remove the next reference object in this queue, blocking until either
  80.      * one becomes available or the given timeout period expires.
  81.      *
  82.      * @param timeout   If positive, block for up <code>timeout</code>
  83.      *                  milliseconds while waiting for a reference to be
  84.      *                  added to this queue.  If zero, block indefinitely.
  85.      *
  86.      * @return  A reference, if one was available, otherwise
  87.      *          <code>null</code>.
  88.      *
  89.      * @exception  IllegalArgumentException
  90.      *             If the value of the timeout argument is negative
  91.      *
  92.      * @exception  InterruptedException
  93.      *             If the timeout wait is interrupted
  94.      */
  95.     public Reference remove(long timeout)
  96.     throws IllegalArgumentException, InterruptedException
  97.     {
  98.     if (timeout < 0) {
  99.         throw new IllegalArgumentException("Timeout value is negative");
  100.     }
  101.     synchronized (pendingLock) {
  102.         Reference r = reallyPoll();
  103.         if (r != null) return r;
  104.         for (;;) {
  105.         pendingLock.wait(timeout);
  106.         r = reallyPoll();
  107.         if (r != null) return r;
  108.         if (timeout != 0) return null;
  109.         }
  110.     }
  111.     }
  112.  
  113.     /**
  114.      * Remove the next reference object in this queue, blocking until one
  115.      * becomes available.
  116.      *
  117.      * @exception  InterruptedException  If the wait is interrupted
  118.      */
  119.     public Reference remove() throws InterruptedException {
  120.     return remove(0);
  121.     }
  122.  
  123. }
  124.