Index: src/java/org/apache/commons/httpclient/util/IdleConnectionTimeoutThread.java =================================================================== --- src/java/org/apache/commons/httpclient/util/IdleConnectionTimeoutThread.java (revision 354387) +++ src/java/org/apache/commons/httpclient/util/IdleConnectionTimeoutThread.java (working copy) @@ -45,7 +45,7 @@ private List connectionManagers = new ArrayList(); - private boolean shutdown = false; + private volatile boolean shutdown = false; private long timeoutInterval = 1000; @@ -62,11 +62,13 @@ * * @param connectionManager The connection manager to add */ - public synchronized void addConnectionManager(HttpConnectionManager connectionManager) { + public void addConnectionManager(HttpConnectionManager connectionManager) { if (shutdown) { throw new IllegalStateException("IdleConnectionTimeoutThread has been shutdown"); } - this.connectionManagers.add(connectionManager); + synchronized(this) { + this.connectionManagers.add(connectionManager); + } } /** @@ -75,38 +77,45 @@ * * @param connectionManager The connection manager to remove */ - public synchronized void removeConnectionManager(HttpConnectionManager connectionManager) { + public void removeConnectionManager(HttpConnectionManager connectionManager) { if (shutdown) { throw new IllegalStateException("IdleConnectionTimeoutThread has been shutdown"); } - this.connectionManagers.remove(connectionManager); + synchronized(this) { + this.connectionManagers.remove(connectionManager); + } } /** * Closes idle connections. */ - public synchronized void run() { + public void run() { while (!shutdown) { - Iterator iter = connectionManagers.iterator(); - - while (iter.hasNext()) { - HttpConnectionManager connectionManager = (HttpConnectionManager) iter.next(); - connectionManager.closeIdleConnections(connectionTimeout); + long pause; + synchronized(this) { + Iterator iter = connectionManagers.iterator(); + + while (iter.hasNext()) { + HttpConnectionManager connectionManager = (HttpConnectionManager) iter.next(); + connectionManager.closeIdleConnections(connectionTimeout); + } + pause = timeoutInterval; } - try { - this.wait(timeoutInterval); + this.wait(pause); } catch (InterruptedException e) { } } // clear out the connection managers now that we're shutdown - this.connectionManagers.clear(); + synchronized(this) { + this.connectionManagers.clear(); + } } /** * Stops the thread used to close idle connections. This class cannot be used once shutdown. */ - public synchronized void shutdown() { + public void shutdown() { this.shutdown = true; } @@ -121,8 +130,11 @@ if (shutdown) { throw new IllegalStateException("IdleConnectionTimeoutThread has been shutdown"); } - this.connectionTimeout = connectionTimeout; + synchronized(this) { + this.connectionTimeout = connectionTimeout; + } } + /** * Sets the interval used by this class between closing idle connections. Idle * connections will be closed every timeoutInterval milliseconds. @@ -133,7 +145,9 @@ if (shutdown) { throw new IllegalStateException("IdleConnectionTimeoutThread has been shutdown"); } - this.timeoutInterval = timeoutInterval; + synchronized(this) { + this.timeoutInterval = timeoutInterval; + } } }