HTTPCLIENT-677: switching to new WaitingThread class

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@607289 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Roland Weber 2007-12-28 17:08:13 +00:00
parent caf0b10534
commit a453692c09
3 changed files with 19 additions and 42 deletions

View File

@ -90,31 +90,6 @@ public class ConnPoolByRoute extends AbstractConnPool {
/**
* A thread and the pool in which it is waiting.
* <!-- @@@ will be revised for HTTPCLIENT-677 -->
*/
protected static class WaitingThread {
/** The thread that is waiting for an entry. */
public Thread thread;
/** The route specific pool the thread is waiting for. */
public RouteSpecificPool pool;
/**
* Indicates the source of an interruption.
* Set to <code>true</code> inside
* {@link #notifyWaitingThread(RouteSpecificPool)}
* and {@link #shutdown shutdown()}
* before the thread is interrupted.
* If not set, the thread was interrupted from the outside.
*/
public boolean interruptedByConnectionPool = false;
}
/** /**
* Creates a new connection pool, managed by route. * Creates a new connection pool, managed by route.
* *
@ -258,11 +233,12 @@ public class ConnPoolByRoute extends AbstractConnPool {
} }
if (waitingThread == null) { if (waitingThread == null) {
waitingThread = new WaitingThread(); waitingThread = new WaitingThread
waitingThread.pool = rospl; (poolLock.newCondition(), rospl);
waitingThread.thread = Thread.currentThread(); //@@@waitingThread.pool = rospl;
//@@@waitingThread.thread = Thread.currentThread();
} else { } else {
waitingThread.interruptedByConnectionPool = false; waitingThread.interruptedByConnectionPool = false;//@@@
} }
if (useTimeout) { if (useTimeout) {
@ -271,7 +247,8 @@ public class ConnPoolByRoute extends AbstractConnPool {
rospl.queueThread(waitingThread); rospl.queueThread(waitingThread);
waitingThreads.add(waitingThread); waitingThreads.add(waitingThread);
poolCondition.await(timeToWait, TimeUnit.MILLISECONDS); //@@@ poolCondition.await(timeToWait, TimeUnit.MILLISECONDS);
waitingThread.await(timeToWait); //@@@, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
if (!waitingThread.interruptedByConnectionPool) { if (!waitingThread.interruptedByConnectionPool) {
@ -547,7 +524,7 @@ public class ConnPoolByRoute extends AbstractConnPool {
LOG.debug("Notifying thread waiting on any pool."); LOG.debug("Notifying thread waiting on any pool.");
} }
waitingThread = waitingThreads.remove(); waitingThread = waitingThreads.remove();
waitingThread.pool.removeThread(waitingThread); waitingThread.getPool().removeThread(waitingThread);
} else if (LOG.isDebugEnabled()) { } else if (LOG.isDebugEnabled()) {
LOG.debug("Notifying no-one, there are no waiting threads"); LOG.debug("Notifying no-one, there are no waiting threads");
@ -555,7 +532,7 @@ public class ConnPoolByRoute extends AbstractConnPool {
if (waitingThread != null) { if (waitingThread != null) {
waitingThread.interruptedByConnectionPool = true; waitingThread.interruptedByConnectionPool = true;
waitingThread.thread.interrupt(); waitingThread.getThread().interrupt(); //@@@ HTTPCLIENT-677
} }
} finally { } finally {
@ -610,7 +587,7 @@ public class ConnPoolByRoute extends AbstractConnPool {
WaitingThread waiter = iwth.next(); WaitingThread waiter = iwth.next();
iwth.remove(); iwth.remove();
waiter.interruptedByConnectionPool = true; waiter.interruptedByConnectionPool = true;
waiter.thread.interrupt(); waiter.getThread().interrupt(); //@@@ HTTPCLIENT-677
} }
routeToPool.clear(); routeToPool.clear();

View File

@ -53,7 +53,7 @@ public class RouteSpecificPool {
protected LinkedList<BasicPoolEntry> freeEntries; protected LinkedList<BasicPoolEntry> freeEntries;
/** The list of threads waiting for this pool. */ /** The list of threads waiting for this pool. */
protected Queue<ConnPoolByRoute.WaitingThread> waitingThreads; protected Queue<WaitingThread> waitingThreads;
/** The number of created entries. */ /** The number of created entries. */
protected int numEntries; protected int numEntries;
@ -67,7 +67,7 @@ public class RouteSpecificPool {
public RouteSpecificPool(HttpRoute r) { public RouteSpecificPool(HttpRoute r) {
this.route = r; this.route = r;
this.freeEntries = new LinkedList<BasicPoolEntry>(); this.freeEntries = new LinkedList<BasicPoolEntry>();
this.waitingThreads = new LinkedList<ConnPoolByRoute.WaitingThread>(); this.waitingThreads = new LinkedList<WaitingThread>();
this.numEntries = 0; this.numEntries = 0;
} }
@ -207,7 +207,7 @@ public class RouteSpecificPool {
* *
* @param wt the waiting thread * @param wt the waiting thread
*/ */
public void queueThread(ConnPoolByRoute.WaitingThread wt) { public void queueThread(WaitingThread wt) {
if (wt == null) { if (wt == null) {
throw new IllegalArgumentException throw new IllegalArgumentException
("Waiting thread must not be null."); ("Waiting thread must not be null.");
@ -232,7 +232,7 @@ public class RouteSpecificPool {
* *
* @return a waiting thread, or <code>null</code> if there is none * @return a waiting thread, or <code>null</code> if there is none
*/ */
public ConnPoolByRoute.WaitingThread dequeueThread() { public WaitingThread dequeueThread() {
return this.waitingThreads.poll(); return this.waitingThreads.poll();
} }
@ -242,7 +242,7 @@ public class RouteSpecificPool {
* *
* @param wt the waiting thread * @param wt the waiting thread
*/ */
public void removeThread(ConnPoolByRoute.WaitingThread wt) { public void removeThread(WaitingThread wt) {
if (wt == null) if (wt == null)
return; return;

View File

@ -68,7 +68,8 @@ public class WaitingThread {
* before the thread is interrupted. * before the thread is interrupted.
* If not set, the thread was interrupted from the outside. * If not set, the thread was interrupted from the outside.
*/ */
private boolean interruptedByConnectionPool; //@@@ to be removed in HTTPCLIENT-677
/*default@@@*/ boolean interruptedByConnectionPool;
/** /**
@ -100,10 +101,11 @@ public class WaitingThread {
* *
* @see #wakeup * @see #wakeup
*/ */
public void await(int timeout) public void await(long timeout)
throws InterruptedException { throws InterruptedException {
//@@@ check timeout for negative, or assume overflow? //@@@ check timeout for negative, or assume overflow?
//@@@ for now, leave the check to the condition
// This is only a sanity check. We cannot not synchronize here, // This is only a sanity check. We cannot not synchronize here,
// the lock would not be released on calling cond.await() below. // the lock would not be released on calling cond.await() below.
@ -117,8 +119,6 @@ public class WaitingThread {
this.waiter = Thread.currentThread(); this.waiter = Thread.currentThread();
try { try {
//@@@ how to convert the int timeout to the long argument?
//@@@ (timeout & 0xffffffffL)? or check for negative above?
this.cond.await(timeout, TimeUnit.MILLISECONDS); this.cond.await(timeout, TimeUnit.MILLISECONDS);
} finally { } finally {
this.waiter = null; this.waiter = null;