From 43942a0ea98e9e743a8238c7027970061b6b993a Mon Sep 17 00:00:00 2001 From: remm Date: Tue, 25 Jul 2006 14:19:32 +0000 Subject: [PATCH] - Experiment with using concurrent maps in places which may be concurrently accessed. git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@425399 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/catalina/core/ApplicationContext.java | 80 +++++++++------------- java/org/apache/catalina/session/ManagerBase.java | 30 ++++---- .../apache/catalina/session/StandardManager.java | 2 +- .../apache/catalina/session/StandardSession.java | 5 +- 4 files changed, 50 insertions(+), 67 deletions(-) diff --git a/java/org/apache/catalina/core/ApplicationContext.java b/java/org/apache/catalina/core/ApplicationContext.java index 8d2d7fdb4..6cff488e7 100644 --- a/java/org/apache/catalina/core/ApplicationContext.java +++ b/java/org/apache/catalina/core/ApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 1999,2004 The Apache Software Foundation. + * Copyright 1999,2004-2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,9 +24,10 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; -import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import javax.naming.Binding; import javax.naming.NamingException; @@ -87,13 +88,13 @@ public class ApplicationContext /** * The context attributes for this context. */ - private HashMap attributes = new HashMap(); + private Map attributes = new ConcurrentHashMap(); /** * List of read only attributes for this context. */ - private HashMap readOnlyAttributes = new HashMap(); + private Map readOnlyAttributes = new ConcurrentHashMap(); /** @@ -118,7 +119,7 @@ public class ApplicationContext /** * The merged context initialization parameters for this Context. */ - private HashMap parameters = null; + private Map parameters = null; /** @@ -172,9 +173,7 @@ public class ApplicationContext */ public Object getAttribute(String name) { - synchronized (attributes) { - return (attributes.get(name)); - } + return (attributes.get(name)); } @@ -185,9 +184,7 @@ public class ApplicationContext */ public Enumeration getAttributeNames() { - synchronized (attributes) { - return new Enumerator(attributes.keySet(), true); - } + return new Enumerator(attributes.keySet(), true); } @@ -258,9 +255,8 @@ public class ApplicationContext public String getInitParameter(final String name) { mergeParameters(); - synchronized (parameters) { - return ((String) parameters.get(name)); - } + return ((String) parameters.get(name)); + } @@ -271,9 +267,7 @@ public class ApplicationContext public Enumeration getInitParameterNames() { mergeParameters(); - synchronized (parameters) { - return (new Enumerator(parameters.keySet())); - } + return (new Enumerator(parameters.keySet())); } @@ -688,17 +682,15 @@ public class ApplicationContext boolean found = false; // Remove the specified attribute - synchronized (attributes) { - // Check for read only attribute - if (readOnlyAttributes.containsKey(name)) - return; - found = attributes.containsKey(name); - if (found) { - value = attributes.get(name); - attributes.remove(name); - } else { - return; - } + // Check for read only attribute + if (readOnlyAttributes.containsKey(name)) + return; + found = attributes.containsKey(name); + if (found) { + value = attributes.get(name); + attributes.remove(name); + } else { + return; } // Notify interested application event listeners @@ -754,15 +746,13 @@ public class ApplicationContext boolean replaced = false; // Add or replace the specified attribute - synchronized (attributes) { - // Check for read only attribute - if (readOnlyAttributes.containsKey(name)) - return; - oldValue = attributes.get(name); - if (oldValue != null) - replaced = true; - attributes.put(name, value); - } + // Check for read only attribute + if (readOnlyAttributes.containsKey(name)) + return; + oldValue = attributes.get(name); + if (oldValue != null) + replaced = true; + attributes.put(name, value); // Notify interested application event listeners Object listeners[] = context.getApplicationEventListeners(); @@ -822,11 +812,9 @@ public class ApplicationContext // Create list of attributes to be removed ArrayList list = new ArrayList(); - synchronized (attributes) { - Iterator iter = attributes.keySet().iterator(); - while (iter.hasNext()) { - list.add(iter.next()); - } + Iterator iter = attributes.keySet().iterator(); + while (iter.hasNext()) { + list.add(iter.next()); } // Remove application originated attributes @@ -855,10 +843,8 @@ public class ApplicationContext */ void setAttributeReadOnly(String name) { - synchronized (attributes) { - if (attributes.containsKey(name)) - readOnlyAttributes.put(name, name); - } + if (attributes.containsKey(name)) + readOnlyAttributes.put(name, name); } @@ -915,7 +901,7 @@ public class ApplicationContext if (parameters != null) return; - HashMap results = new HashMap(); + Map results = new ConcurrentHashMap(); String names[] = context.findParameters(); for (int i = 0; i < names.length; i++) results.put(names[i], context.findParameter(names[i])); diff --git a/java/org/apache/catalina/session/ManagerBase.java b/java/org/apache/catalina/session/ManagerBase.java index 7100e8b0e..a5bad5790 100644 --- a/java/org/apache/catalina/session/ManagerBase.java +++ b/java/org/apache/catalina/session/ManagerBase.java @@ -1,5 +1,5 @@ /* - * Copyright 1999,2004 The Apache Software Foundation. + * Copyright 1999,2004-2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,7 +33,9 @@ import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; import javax.management.MBeanRegistration; import javax.management.MBeanServer; @@ -171,7 +173,7 @@ public abstract class ManagerBase implements Manager, MBeanRegistration { * The set of currently active Sessions for this Manager, keyed by * session identifier. */ - protected HashMap sessions = new HashMap(); + protected Map sessions = new ConcurrentHashMap(); // Number of sessions created by this manager protected int sessionCounter=0; @@ -731,11 +733,10 @@ public abstract class ManagerBase implements Manager, MBeanRegistration { */ public void add(Session session) { - synchronized (sessions) { - sessions.put(session.getIdInternal(), session); - if( sessions.size() > maxActive ) { - maxActive=sessions.size(); - } + sessions.put(session.getIdInternal(), session); + int size = sessions.size(); + if( size > maxActive ) { + maxActive = size; } } @@ -854,10 +855,7 @@ public abstract class ManagerBase implements Manager, MBeanRegistration { if (id == null) return (null); - synchronized (sessions) { - Session session = (Session) sessions.get(id); - return (session); - } + return (Session) sessions.get(id); } @@ -885,9 +883,7 @@ public abstract class ManagerBase implements Manager, MBeanRegistration { */ public void remove(Session session) { - synchronized (sessions) { - sessions.remove(session.getIdInternal()); - } + sessions.remove(session.getIdInternal()); } @@ -1134,8 +1130,8 @@ public abstract class ManagerBase implements Manager, MBeanRegistration { */ public String listSessionIds() { StringBuffer sb=new StringBuffer(); - Iterator keys=sessions.keySet().iterator(); - while( keys.hasNext() ) { + Iterator keys = sessions.keySet().iterator(); + while (keys.hasNext()) { sb.append(keys.next()).append(" "); } return sb.toString(); @@ -1150,7 +1146,7 @@ public abstract class ManagerBase implements Manager, MBeanRegistration { * @return The attribute value, if found, null otherwise */ public String getSessionAttribute( String sessionId, String key ) { - Session s=(Session)sessions.get(sessionId); + Session s = (Session) sessions.get(sessionId); if( s==null ) { if(log.isInfoEnabled()) log.info("Session not found " + sessionId); diff --git a/java/org/apache/catalina/session/StandardManager.java b/java/org/apache/catalina/session/StandardManager.java index 7af7884e4..3a785eca7 100644 --- a/java/org/apache/catalina/session/StandardManager.java +++ b/java/org/apache/catalina/session/StandardManager.java @@ -1,5 +1,5 @@ /* - * Copyright 1999,2004 The Apache Software Foundation. + * Copyright 1999,2004-2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/java/org/apache/catalina/session/StandardSession.java b/java/org/apache/catalina/session/StandardSession.java index de403ac6f..55e03c1cb 100644 --- a/java/org/apache/catalina/session/StandardSession.java +++ b/java/org/apache/catalina/session/StandardSession.java @@ -1,5 +1,5 @@ /* - * Copyright 1999,2004 The Apache Software Foundation. + * Copyright 1999,2004-2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; import javax.servlet.ServletContext; @@ -122,7 +123,7 @@ public class StandardSession /** * The collection of user data attributes associated with this Session. */ - protected Map attributes = new Hashtable(); + protected Map attributes = new ConcurrentHashMap(); /** -- 2.11.0