FileDocCategorySizeDatePackage
ReferenceQueue.javaAPI DocJava SE 6 API3929Tue Jun 10 00:25:38 BST 2008java.lang.ref

ReferenceQueue

public class ReferenceQueue extends Object
Reference queues, to which registered reference objects are appended by the garbage collector after the appropriate reachability changes are detected.
version
1.25, 11/17/05
author
Mark Reinhold
since
1.2

Fields Summary
static ReferenceQueue
NULL
static ReferenceQueue
ENQUEUED
private Lock
lock
private Reference
head
private long
queueLength
Constructors Summary
public ReferenceQueue()
Constructs a new reference-object queue.

 
Methods Summary
booleanenqueue(java.lang.ref.Reference r)


         	/* Called only by Reference class */
	synchronized (r) {
	    if (r.queue == ENQUEUED) return false;
	    synchronized (lock) {
		r.queue = ENQUEUED;
		r.next = (head == null) ? r : head;
		head = r;
		queueLength++;
                if (r instanceof FinalReference) {
                    sun.misc.VM.addFinalRefCount(1);
                }
		lock.notifyAll();
		return true;
	    }
	}
    
public java.lang.ref.Referencepoll()
Polls this queue to see if a reference object is available. If one is available without further delay then it is removed from the queue and returned. Otherwise this method immediately returns null.

return
A reference object, if one was immediately available, otherwise null

	synchronized (lock) {
	    return reallyPoll();
	}
    
private java.lang.ref.ReferencereallyPoll()

	/* Must hold lock */
	if (head != null) {
	    Reference<? extends T> r = head;
	    head = (r.next == r) ? null : r.next;
	    r.queue = NULL;
	    r.next = r;
	    queueLength--;
            if (r instanceof FinalReference) {
                sun.misc.VM.addFinalRefCount(-1);
            }
	    return r;
	}
	return null;
    
public java.lang.ref.Referenceremove(long timeout)
Removes the next reference object in this queue, blocking until either one becomes available or the given timeout period expires.

This method does not offer real-time guarantees: It schedules the timeout as if by invoking the {@link Object#wait(long)} method.

param
timeout If positive, block for up to timeout milliseconds while waiting for a reference to be added to this queue. If zero, block indefinitely.
return
A reference object, if one was available within the specified timeout period, otherwise null
throws
IllegalArgumentException If the value of the timeout argument is negative
throws
InterruptedException If the timeout wait is interrupted

	if (timeout < 0) {
	    throw new IllegalArgumentException("Negative timeout value");
	}
	synchronized (lock) {
	    Reference<? extends T> r = reallyPoll();
	    if (r != null) return r;
	    for (;;) {
		lock.wait(timeout);
		r = reallyPoll();
		if (r != null) return r;
		if (timeout != 0) return null;
	    }
	}
    
public java.lang.ref.Referenceremove()
Removes the next reference object in this queue, blocking until one becomes available.

return
A reference object, blocking until one becomes available
throws
InterruptedException If the wait is interrupted

	return remove(0);