From f1f1a7a72ccd3a7839e536a9746467497da2dda0 Mon Sep 17 00:00:00 2001 From: Roland Weber Date: Fri, 14 Dec 2007 19:05:02 +0000 Subject: [PATCH] cleanup and accessors in RouteSpecificPool git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@604259 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/impl/conn/tsccm/ConnPoolByRoute.java | 12 +-- .../impl/conn/tsccm/RouteSpecificPool.java | 73 ++++++++++++++++--- 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java b/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java index fa292f019..ee6ede0d2 100644 --- a/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java +++ b/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java @@ -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"); diff --git a/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RouteSpecificPool.java b/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RouteSpecificPool.java index abfe42d55..0c7328f81 100644 --- a/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RouteSpecificPool.java +++ b/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RouteSpecificPool.java @@ -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 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 freeEntries; /** The list of threads waiting for this pool. */ - /*private@@@ currently still default*/ LinkedList waitingThreads; + protected Queue 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(); - this.waitingThreads = new LinkedList(); + this.waitingThreads = new LinkedList(); 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 true if there is a waiting thread, + * false otherwise + */ + public boolean hasThread() { + return !this.waitingThreads.isEmpty(); + } + + + /** + * Obtains and removes a waiting thread. + * + * @return a waiting thread, or null 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