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

WaitingThread

public class WaitingThread extends Object
Represents a thread waiting for a connection. This class implements throwaway objects. It is instantiated whenever a thread needs to wait. Instances are not re-used, except if the waiting thread experiences a spurious wakeup and continues to wait.
All methods assume external synchronization on the condition passed to the constructor. Instances of this class do not synchronize access!
author
Roland Weber

Fields Summary
private final Condition
cond
The condition on which the thread is waiting.
private final RouteSpecificPool
pool
The route specific pool on which the thread is waiting.
private Thread
waiter
The thread that is waiting for an entry.
private boolean
aborted
True if this was interrupted.
Constructors Summary
public WaitingThread(Condition cond, RouteSpecificPool pool)
Creates a new entry for a waiting thread.

param
cond the condition for which to wait
param
pool the pool on which the thread will be waiting, or null


        if (cond == null) {
            throw new IllegalArgumentException("Condition must not be null.");
        }

        this.cond = cond;
        this.pool = pool;
    
Methods Summary
public booleanawait(java.util.Date deadline)
Blocks the calling thread. This method returns when the thread is notified or interrupted, if a timeout occurrs, or if there is a spurious wakeup.
This method assumes external synchronization.

param
deadline when to time out, or null for no timeout
return
true if the condition was satisfied, false in case of a timeout. Typically, a call to {@link #wakeup} is used to indicate that the condition was satisfied. Since the condition is accessible outside, this cannot be guaranteed though.
throws
InterruptedException if the waiting thread was interrupted
see
#wakeup


        // This is only a sanity check. We cannot synchronize here,
        // the lock would not be released on calling cond.await() below.
        if (this.waiter != null) {
            throw new IllegalStateException
                ("A thread is already waiting on this object." +
                 "\ncaller: " + Thread.currentThread() +
                 "\nwaiter: " + this.waiter);
        }

        if (aborted)
            throw new InterruptedException("Operation interrupted");
        
        this.waiter = Thread.currentThread();

        boolean success = false;
        try {
            if (deadline != null) {
                success = this.cond.awaitUntil(deadline);
            } else {
                this.cond.await();
                success = true;
            }
            if (aborted)
                throw new InterruptedException("Operation interrupted");
        } finally {
            this.waiter = null;
        }
        return success;

    
public final java.util.concurrent.locks.ConditiongetCondition()
Obtains the condition.

return
the condition on which to wait, never null

        // not synchronized
        return this.cond;
    
public final org.apache.http.impl.conn.tsccm.RouteSpecificPoolgetPool()
Obtains the pool, if there is one.

return
the pool on which a thread is or was waiting, or null

        // not synchronized
        return this.pool;
    
public final java.lang.ThreadgetThread()
Obtains the thread, if there is one.

return
the thread which is waiting, or null

        // not synchronized
        return this.waiter;
    
public voidinterrupt()

        aborted = true;
        this.cond.signalAll();
    
public voidwakeup()
Wakes up the waiting thread.
This method assumes external synchronization.


        // If external synchronization and pooling works properly,
        // this cannot happen. Just a sanity check.
        if (this.waiter == null) {
            throw new IllegalStateException
                ("Nobody waiting on this object.");
        }

        // One condition might be shared by several WaitingThread instances.
        // It probably isn't, but just in case: wake all, not just one.
        this.cond.signalAll();