FileDocCategorySizeDatePackage
RefQueueWorker.javaAPI DocAndroid 1.5 API4569Wed May 06 22:41:10 BST 2009org.apache.http.impl.conn.tsccm

RefQueueWorker

public class RefQueueWorker extends Object implements Runnable
A worker thread for processing queued references. {@link Reference Reference}s can be {@link ReferenceQueue queued} automatically by the garbage collector. If that feature is used, a daemon thread should be executing this worker. It will pick up the queued references and pass them on to a handler for appropriate processing.

Fields Summary
private final Log
log
protected final ReferenceQueue
refQueue
The reference queue to monitor.
protected final RefQueueHandler
refHandler
The handler for the references found.
protected volatile Thread
workerThread
The thread executing this handler. This attribute is also used as a shutdown indicator.
Constructors Summary
public RefQueueWorker(ReferenceQueue queue, RefQueueHandler handler)
Instantiates a new worker to listen for lost connections.

param
queue the queue on which to wait for references
param
handler the handler to pass the references to



                                           
         
        if (queue == null) {
            throw new IllegalArgumentException("Queue must not be null.");
        }
        if (handler == null) {
            throw new IllegalArgumentException("Handler must not be null.");
        }

        refQueue   = queue;
        refHandler = handler;
    
Methods Summary
public voidrun()
The main loop of this worker. If initialization succeeds, this method will only return after {@link #shutdown shutdown()}. Only one thread can execute the main loop at any time.


        if (this.workerThread == null) {
            this.workerThread = Thread.currentThread();
        }

        while (this.workerThread == Thread.currentThread()) {
            try {
                // remove the next reference and process it
                Reference<?> ref = refQueue.remove();
                refHandler.handleReference(ref);
            } catch (InterruptedException e) {
                //@@@ is logging really necessary? this here is the
                //@@@ only reason for having a log in this class
                if (log.isDebugEnabled()) {
                    log.debug(this.toString() + " interrupted", e);
                }
            }
        }
    
public voidshutdown()
Shuts down this worker. It can be re-started afterwards by another call to {@link #run run()}.

        Thread wt = this.workerThread;
        if (wt != null) {
            this.workerThread = null; // indicate shutdown
            wt.interrupt();
        }
    
public java.lang.StringtoString()
Obtains a description of this worker.

return
a descriptive string for this worker

        return "RefQueueWorker::" + this.workerThread;