HTTPCLIENT-903 Use ConcurrentHashMap instead of [Linked]HashMap for thread-safety

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@897641 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2010-01-10 14:23:56 +00:00
parent c59bd4f3ac
commit 14b26e2a33
3 changed files with 32 additions and 39 deletions

View File

@ -27,12 +27,11 @@
package org.apache.http.auth;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.annotation.GuardedBy;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.params.HttpParams;
@ -46,12 +45,11 @@
@ThreadSafe
public final class AuthSchemeRegistry {
@GuardedBy("this")
private final Map<String,AuthSchemeFactory> registeredSchemes;
private final ConcurrentHashMap<String,AuthSchemeFactory> registeredSchemes;
public AuthSchemeRegistry() {
super();
this.registeredSchemes = new LinkedHashMap<String,AuthSchemeFactory>();
this.registeredSchemes = new ConcurrentHashMap<String,AuthSchemeFactory>();
}
/**
@ -69,7 +67,7 @@ public AuthSchemeRegistry() {
*
* @see #getAuthScheme
*/
public synchronized void register(
public void register(
final String name,
final AuthSchemeFactory factory) {
if (name == null) {
@ -87,7 +85,7 @@ public synchronized void register(
*
* @param name the identifier of the class to unregister
*/
public synchronized void unregister(final String name) {
public void unregister(final String name) {
if (name == null) {
throw new IllegalArgumentException("Name may not be null");
}
@ -105,7 +103,7 @@ public synchronized void unregister(final String name) {
*
* @throws IllegalStateException if a scheme with the given name cannot be found
*/
public synchronized AuthScheme getAuthScheme(final String name, final HttpParams params)
public AuthScheme getAuthScheme(final String name, final HttpParams params)
throws IllegalStateException {
if (name == null) {
@ -120,12 +118,12 @@ public synchronized AuthScheme getAuthScheme(final String name, final HttpParams
}
/**
* Obtains a list containing names of all registered {@link AuthScheme authentication
* schemes} in their default order.
* Obtains a list containing the names of all registered {@link AuthScheme authentication
* schemes}
*
* @return list of registered scheme names
*/
public synchronized List<String> getSchemeNames() {
public List<String> getSchemeNames() {
return new ArrayList<String>(registeredSchemes.keySet());
}
@ -135,7 +133,7 @@ public synchronized List<String> getSchemeNames() {
*
* @param map authentication schemes
*/
public synchronized void setItems(final Map<String, AuthSchemeFactory> map) {
public void setItems(final Map<String, AuthSchemeFactory> map) {
if (map == null) {
return;
}

View File

@ -26,10 +26,9 @@
package org.apache.http.conn.params;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.annotation.GuardedBy;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.conn.routing.HttpRoute;
@ -48,15 +47,13 @@ public final class ConnPerRouteBean implements ConnPerRoute {
/** The default maximum number of connections allowed per host */
public static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 2; // Per RFC 2616 sec 8.1.4
@GuardedBy("this")
private final Map<HttpRoute, Integer> maxPerHostMap;
private final ConcurrentHashMap<HttpRoute, Integer> maxPerHostMap;
@GuardedBy("this")
private int defaultMax;
private volatile int defaultMax;
public ConnPerRouteBean(int defaultMax) {
super();
this.maxPerHostMap = new HashMap<HttpRoute, Integer>();
this.maxPerHostMap = new ConcurrentHashMap<HttpRoute, Integer>();
setDefaultMaxPerRoute(defaultMax);
}
@ -65,18 +62,18 @@ public ConnPerRouteBean() {
}
@Deprecated
public synchronized int getDefaultMax() {
public int getDefaultMax() {
return this.defaultMax;
}
/**
* @since 4.1
*/
public synchronized int getDefaultMaxPerRoute() {
public int getDefaultMaxPerRoute() {
return this.defaultMax;
}
public synchronized void setDefaultMaxPerRoute(int max) {
public void setDefaultMaxPerRoute(int max) {
if (max < 1) {
throw new IllegalArgumentException
("The maximum must be greater than 0.");
@ -84,7 +81,7 @@ public synchronized void setDefaultMaxPerRoute(int max) {
this.defaultMax = max;
}
public synchronized void setMaxForRoute(final HttpRoute route, int max) {
public void setMaxForRoute(final HttpRoute route, int max) {
if (route == null) {
throw new IllegalArgumentException
("HTTP route may not be null.");
@ -96,7 +93,7 @@ public synchronized void setMaxForRoute(final HttpRoute route, int max) {
this.maxPerHostMap.put(route, Integer.valueOf(max));
}
public synchronized int getMaxForRoute(final HttpRoute route) {
public int getMaxForRoute(final HttpRoute route) {
if (route == null) {
throw new IllegalArgumentException
("HTTP route may not be null.");
@ -109,7 +106,7 @@ public synchronized int getMaxForRoute(final HttpRoute route) {
}
}
public synchronized void setMaxForRoutes(final Map<HttpRoute, Integer> map) {
public void setMaxForRoutes(final Map<HttpRoute, Integer> map) {
if (map == null) {
return;
}
@ -118,7 +115,7 @@ public synchronized void setMaxForRoutes(final Map<HttpRoute, Integer> map) {
}
@Override
public synchronized String toString() {
public String toString() {
return this.maxPerHostMap.toString();
}

View File

@ -27,11 +27,10 @@
package org.apache.http.conn.scheme;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.annotation.GuardedBy;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.HttpHost;
@ -46,15 +45,14 @@
public final class SchemeRegistry {
/** The available schemes in this registry. */
@GuardedBy("this")
private final Map<String,Scheme> registeredSchemes;
private final ConcurrentHashMap<String,Scheme> registeredSchemes;
/**
* Creates a new, empty scheme registry.
*/
public SchemeRegistry() {
super();
registeredSchemes = new LinkedHashMap<String,Scheme>();
registeredSchemes = new ConcurrentHashMap<String,Scheme>();
}
/**
@ -67,7 +65,7 @@ public SchemeRegistry() {
* @throws IllegalStateException
* if the scheme with the given name is not registered
*/
public synchronized final Scheme getScheme(String name) {
public final Scheme getScheme(String name) {
Scheme found = get(name);
if (found == null) {
throw new IllegalStateException
@ -87,7 +85,7 @@ public synchronized final Scheme getScheme(String name) {
* @throws IllegalStateException
* if a scheme with the respective name is not registered
*/
public synchronized final Scheme getScheme(HttpHost host) {
public final Scheme getScheme(HttpHost host) {
if (host == null) {
throw new IllegalArgumentException("Host must not be null.");
}
@ -102,7 +100,7 @@ public synchronized final Scheme getScheme(HttpHost host) {
* @return the scheme, or
* <code>null</code> if there is none by this name
*/
public synchronized final Scheme get(String name) {
public final Scheme get(String name) {
if (name == null)
throw new IllegalArgumentException("Name must not be null.");
@ -122,7 +120,7 @@ public synchronized final Scheme get(String name) {
* @return the scheme previously registered with that name, or
* <code>null</code> if none was registered
*/
public synchronized final Scheme register(Scheme sch) {
public final Scheme register(Scheme sch) {
if (sch == null)
throw new IllegalArgumentException("Scheme must not be null.");
@ -138,7 +136,7 @@ public synchronized final Scheme register(Scheme sch) {
* @return the unregistered scheme, or
* <code>null</code> if there was none
*/
public synchronized final Scheme unregister(String name) {
public final Scheme unregister(String name) {
if (name == null)
throw new IllegalArgumentException("Name must not be null.");
@ -149,11 +147,11 @@ public synchronized final Scheme unregister(String name) {
}
/**
* Obtains the names of the registered schemes in their default order.
* Obtains the names of the registered schemes.
*
* @return List containing registered scheme names.
*/
public synchronized final List<String> getSchemeNames() {
public final List<String> getSchemeNames() {
return new ArrayList<String>(registeredSchemes.keySet());
}
@ -163,7 +161,7 @@ public synchronized final List<String> getSchemeNames() {
*
* @param map protocol schemes
*/
public synchronized void setItems(final Map<String, Scheme> map) {
public void setItems(final Map<String, Scheme> map) {
if (map == null) {
return;
}