2012/9/24 <ma...@apache.org>: > Author: markt > Date: Sun Sep 23 21:09:22 2012 > New Revision: 1389145 > > URL: http://svn.apache.org/viewvc?rev=1389145&view=rev > Log: > Some more low(ish) hanging fruit from the allocation hit list. This accounts > for ~8% due to the way Thread stores names. > > Added: > > tomcat/trunk/java/org/apache/tomcat/util/collections/ConcurrentWeakHashMap.java > (with props) > Modified: > tomcat/trunk/java/org/apache/catalina/connector/CoyoteAdapter.java >
> Added: > tomcat/trunk/java/org/apache/tomcat/util/collections/ConcurrentWeakHashMap.java > URL: > http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/collections/ConcurrentWeakHashMap.java?rev=1389145&view=auto > ============================================================================== > --- > tomcat/trunk/java/org/apache/tomcat/util/collections/ConcurrentWeakHashMap.java > (added) > +++ > tomcat/trunk/java/org/apache/tomcat/util/collections/ConcurrentWeakHashMap.java > Sun Sep 23 21:09:22 2012 > @@ -0,0 +1,53 @@ > +package org.apache.tomcat.util.collections; > + > +import java.util.WeakHashMap; > +import java.util.concurrent.locks.Lock; > +import java.util.concurrent.locks.ReadWriteLock; > +import java.util.concurrent.locks.ReentrantReadWriteLock; > + > +/** > + * Wraps a WeakHashMap and makes it thread safe when the typical usage is few > + * writes and many reads. This class deliberately does not provide access to > the > + * full Map interface. It only exposes the methods required by Tomcat. > + */ > +public class ConcurrentWeakHashMap<K,V> { > + > + private final ReadWriteLock lock = new ReentrantReadWriteLock(); > + private final Lock readLock = lock.readLock(); > + private final Lock writeLock = lock.writeLock(); > + private final WeakHashMap<K,V> map = new WeakHashMap<>(); > + > + public V get(K k) { > + readLock.lock(); > + try { > + return map.get(k); > + } finally { > + readLock.unlock(); > + } > + } > + > + public V put(K k, V v) { > + writeLock.lock(); > + try { > + return map.put(k, v); > + } finally { > + writeLock.unlock(); > + } > + } > +} -1 for ReadWriteLock. The problem with WeakHashMap is that its get() operation can modify it (as it processes referencequeue). Thus you cannot use ReadWriteLock, but must use plain synchronization here. You may look at the following class that I implemented some time ago. It moves reference queue handling into separate method that is callable as a periodic task. http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/collections/ManagedConcurrentWeakHashMap.java?view=markup (for https://issues.apache.org/bugzilla/show_bug.cgi?id=53085) The ManagedConcurrentWeakHashMap class currently is not used, as I wanted to have more thought/review/testing of it before using it. Best regards, Konstantin Kolinko --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org