cleanup and accessors in RouteSpecificPool

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@604259 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Roland Weber 2007-12-14 19:05:02 +00:00
parent 289c793a54
commit f1f1a7a72c
2 changed files with 69 additions and 16 deletions

View File

@ -240,7 +240,7 @@ public class ConnPoolByRoute extends AbstractConnPool {
startWait = System.currentTimeMillis();
}
rospl.waitingThreads.addLast(waitingThread);
rospl.queueThread(waitingThread);
waitingThreads.add(waitingThread);
wait(timeToWait);
@ -259,8 +259,8 @@ public class ConnPoolByRoute extends AbstractConnPool {
// Either we timed out, experienced a
// "spurious wakeup", or were interrupted by an
// external thread. Regardless we need to
// cleanup for ourselves in the wait queue.
rospl.waitingThreads.remove(waitingThread);
// cleanup for ourselves in the wait queue.
rospl.removeThread(waitingThread);
waitingThreads.remove(waitingThread);
}
@ -455,12 +455,12 @@ public class ConnPoolByRoute extends AbstractConnPool {
// it from all wait queues before interrupting.
WaitingThread waitingThread = null;
if ((rospl != null) && !rospl.waitingThreads.isEmpty()) {
if ((rospl != null) && rospl.hasThread()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Notifying thread waiting on pool. "
+ rospl.getRoute());
}
waitingThread = (WaitingThread) rospl.waitingThreads.removeFirst();
waitingThread = rospl.dequeueThread();
waitingThreads.remove(waitingThread);
} else if (!waitingThreads.isEmpty()) {
@ -468,7 +468,7 @@ public class ConnPoolByRoute extends AbstractConnPool {
LOG.debug("Notifying thread waiting on any pool.");
}
waitingThread = waitingThreads.remove();
waitingThread.pool.waitingThreads.remove(waitingThread);
waitingThread.pool.removeThread(waitingThread);
} else if (LOG.isDebugEnabled()) {
LOG.debug("Notifying no-one, there are no waiting threads");

View File

@ -30,6 +30,7 @@
package org.apache.http.impl.conn.tsccm;
import java.util.Queue;
import java.util.LinkedList;
import org.apache.http.conn.HttpRoute;
@ -41,31 +42,32 @@ import org.apache.http.conn.HttpRoute;
*/
public class RouteSpecificPool {
//@@@ change attribute visibility to protected once it is ensured
//@@@ that there is no direct attribute access within this package
/** The route this pool is for. */
private final HttpRoute route;
protected final HttpRoute route;
/** The list of free entries. */
private LinkedList<BasicPoolEntry> freeEntries;
/**
* The list of free entries.
* This list is managed LIFO, to increase idle times and
* allow for closing connections that are not really needed.
*/
protected LinkedList<BasicPoolEntry> freeEntries;
/** The list of threads waiting for this pool. */
/*private@@@ currently still default*/ LinkedList<Object> waitingThreads;
protected Queue<ConnPoolByRoute.WaitingThread> waitingThreads;
/** The number of created entries. */
private int numEntries;
protected int numEntries;
/**
* Creates a new route-specific pool.
*
* @param r the route for which to pool
*/
*/
public RouteSpecificPool(HttpRoute r) {
this.route = r;
this.freeEntries = new LinkedList<BasicPoolEntry>();
this.waitingThreads = new LinkedList<Object>();
this.waitingThreads = new LinkedList<ConnPoolByRoute.WaitingThread>();
this.numEntries = 0;
}
@ -197,4 +199,55 @@ public class RouteSpecificPool {
}
/**
* Adds a waiting thread.
* This pool makes no attempt to match waiting threads with pool entries.
* It is the caller's responsibility to check that there is no entry
* before adding a waiting thread.
*
* @param wt the waiting thread
*/
public void queueThread(ConnPoolByRoute.WaitingThread wt) {
if (wt == null) {
throw new IllegalArgumentException
("Waiting thread must not be null.");
}
this.waitingThreads.add(wt);
}
/**
* Checks whether there is a waiting thread in this pool.
*
* @return <code>true</code> if there is a waiting thread,
* <code>false</code> otherwise
*/
public boolean hasThread() {
return !this.waitingThreads.isEmpty();
}
/**
* Obtains and removes a waiting thread.
*
* @return a waiting thread, or <code>null</code> if there is none
*/
public ConnPoolByRoute.WaitingThread dequeueThread() {
return this.waitingThreads.poll();
}
/**
* Removes a waiting thread, if it is queued.
*
* @param wt the waiting thread
*/
public void removeThread(ConnPoolByRoute.WaitingThread wt) {
if (wt == null)
return;
this.waitingThreads.remove(wt);
}
} // class RouteSpecificPool