home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 3.3 KB | 124 lines |
- /*
- * @(#)ReferenceQueue.java 1.6 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.lang.ref;
-
-
- /**
- * Reference queues, to which registered reference objects are appended by the
- * garbage collector after the appropriate reachability changes are detected.
- *
- * @see java.lang.ref.Reference
- * @since JDK1.2
- */
-
- public class ReferenceQueue {
-
- static private class Lock { };
- private Lock pendingLock = new Lock();
- private Reference pending = null;
-
- /**
- * Construct a new reference-object queue.
- */
- public ReferenceQueue() { }
-
- boolean enqueue(Reference r) { /* Called only by Reference class */
- synchronized (pendingLock) {
- if (r.queue != null) {
- r.nextPending = pending;
- pending = r;
- pendingLock.notifyAll();
- r.queue = null;
- return true;
- } else {
- return false;
- }
- }
- }
-
- private Reference reallyPoll() { /* Must hold pendingLock */
- if (pending != null) {
- Reference r = pending;
- pending = r.nextPending;
- r.queue = null;
- r.state = ((r instanceof WeakReference)
- ? Reference.ACTIVE : Reference.IDLE);
- r.nextPending = null;
- return r;
- }
- return null;
- }
-
- /**
- * Poll this queue to see if a reference object is available,
- * immediately returning one if so. If the queue is empty, this
- * method immediately returns <code>null</code>.
- *
- * @return A reference, if one was immediately available, otherwise
- * <code>null</code>.
- */
- public Reference poll() {
- synchronized (pendingLock) {
- return reallyPoll();
- }
- }
-
- /**
- * Remove the next reference object in this queue, blocking until either
- * one becomes available or the given timeout period expires.
- *
- * @param timeout If positive, block for up <code>timeout</code>
- * milliseconds while waiting for a reference to be
- * added to this queue. If zero, block indefinitely.
- *
- * @return A reference, if one was available, otherwise
- * <code>null</code>.
- *
- * @exception IllegalArgumentException
- * If the value of the timeout argument is negative
- *
- * @exception InterruptedException
- * If the timeout wait is interrupted
- */
- public Reference remove(long timeout)
- throws IllegalArgumentException, InterruptedException
- {
- if (timeout < 0) {
- throw new IllegalArgumentException("Timeout value is negative");
- }
- synchronized (pendingLock) {
- Reference r = reallyPoll();
- if (r != null) return r;
- for (;;) {
- pendingLock.wait(timeout);
- r = reallyPoll();
- if (r != null) return r;
- if (timeout != 0) return null;
- }
- }
- }
-
- /**
- * Remove the next reference object in this queue, blocking until one
- * becomes available.
- *
- * @exception InterruptedException If the wait is interrupted
- */
- public Reference remove() throws InterruptedException {
- return remove(0);
- }
-
- }
-