From a453692c09172f440a3a8e60820b5986e0e01c07 Mon Sep 17 00:00:00 2001 From: Roland Weber Date: Fri, 28 Dec 2007 17:08:13 +0000 Subject: [PATCH] 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 --- .../http/impl/conn/tsccm/ConnPoolByRoute.java | 43 +++++-------------- .../impl/conn/tsccm/RouteSpecificPool.java | 10 ++--- .../http/impl/conn/tsccm/WaitingThread.java | 8 ++-- 3 files changed, 19 insertions(+), 42 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 13cb85a9a..f6df4ad6c 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 @@ -90,31 +90,6 @@ public class ConnPoolByRoute extends AbstractConnPool { - /** - * A thread and the pool in which it is waiting. - * - */ - 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 true 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. * @@ -258,11 +233,12 @@ public class ConnPoolByRoute extends AbstractConnPool { } if (waitingThread == null) { - waitingThread = new WaitingThread(); - waitingThread.pool = rospl; - waitingThread.thread = Thread.currentThread(); + waitingThread = new WaitingThread + (poolLock.newCondition(), rospl); + //@@@waitingThread.pool = rospl; + //@@@waitingThread.thread = Thread.currentThread(); } else { - waitingThread.interruptedByConnectionPool = false; + waitingThread.interruptedByConnectionPool = false;//@@@ } if (useTimeout) { @@ -271,7 +247,8 @@ public class ConnPoolByRoute extends AbstractConnPool { rospl.queueThread(waitingThread); waitingThreads.add(waitingThread); - poolCondition.await(timeToWait, TimeUnit.MILLISECONDS); + //@@@ poolCondition.await(timeToWait, TimeUnit.MILLISECONDS); + waitingThread.await(timeToWait); //@@@, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { if (!waitingThread.interruptedByConnectionPool) { @@ -547,7 +524,7 @@ public class ConnPoolByRoute extends AbstractConnPool { LOG.debug("Notifying thread waiting on any pool."); } waitingThread = waitingThreads.remove(); - waitingThread.pool.removeThread(waitingThread); + waitingThread.getPool().removeThread(waitingThread); } else if (LOG.isDebugEnabled()) { LOG.debug("Notifying no-one, there are no waiting threads"); @@ -555,7 +532,7 @@ public class ConnPoolByRoute extends AbstractConnPool { if (waitingThread != null) { waitingThread.interruptedByConnectionPool = true; - waitingThread.thread.interrupt(); + waitingThread.getThread().interrupt(); //@@@ HTTPCLIENT-677 } } finally { @@ -610,7 +587,7 @@ public class ConnPoolByRoute extends AbstractConnPool { WaitingThread waiter = iwth.next(); iwth.remove(); waiter.interruptedByConnectionPool = true; - waiter.thread.interrupt(); + waiter.getThread().interrupt(); //@@@ HTTPCLIENT-677 } routeToPool.clear(); 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 0c7328f81..161f15538 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 @@ -53,7 +53,7 @@ public class RouteSpecificPool { protected LinkedList freeEntries; /** The list of threads waiting for this pool. */ - protected Queue waitingThreads; + protected Queue waitingThreads; /** The number of created entries. */ protected int numEntries; @@ -67,7 +67,7 @@ public class RouteSpecificPool { public RouteSpecificPool(HttpRoute r) { this.route = r; this.freeEntries = new LinkedList(); - this.waitingThreads = new LinkedList(); + this.waitingThreads = new LinkedList(); this.numEntries = 0; } @@ -207,7 +207,7 @@ public class RouteSpecificPool { * * @param wt the waiting thread */ - public void queueThread(ConnPoolByRoute.WaitingThread wt) { + public void queueThread(WaitingThread wt) { if (wt == null) { throw new IllegalArgumentException ("Waiting thread must not be null."); @@ -232,7 +232,7 @@ public class RouteSpecificPool { * * @return a waiting thread, or null if there is none */ - public ConnPoolByRoute.WaitingThread dequeueThread() { + public WaitingThread dequeueThread() { return this.waitingThreads.poll(); } @@ -242,7 +242,7 @@ public class RouteSpecificPool { * * @param wt the waiting thread */ - public void removeThread(ConnPoolByRoute.WaitingThread wt) { + public void removeThread(WaitingThread wt) { if (wt == null) return; diff --git a/module-client/src/main/java/org/apache/http/impl/conn/tsccm/WaitingThread.java b/module-client/src/main/java/org/apache/http/impl/conn/tsccm/WaitingThread.java index 2ac5268c3..3500c82e0 100644 --- a/module-client/src/main/java/org/apache/http/impl/conn/tsccm/WaitingThread.java +++ b/module-client/src/main/java/org/apache/http/impl/conn/tsccm/WaitingThread.java @@ -68,7 +68,8 @@ public class WaitingThread { * before the thread is interrupted. * 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 */ - public void await(int timeout) + public void await(long timeout) throws InterruptedException { //@@@ 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, // the lock would not be released on calling cond.await() below. @@ -117,8 +119,6 @@ public class WaitingThread { this.waiter = Thread.currentThread(); try { - //@@@ how to convert the int timeout to the long argument? - //@@@ (timeout & 0xffffffffL)? or check for negative above? this.cond.await(timeout, TimeUnit.MILLISECONDS); } finally { this.waiter = null;