commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r1173922 - /commons/proper/pool/trunk/src/test/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
Date Thu, 22 Sep 2011 01:10:13 GMT
Author: psteitz
Date: Thu Sep 22 01:10:13 2011
New Revision: 1173922

URL: http://svn.apache.org/viewvc?rev=1173922&view=rev
Log:
Improved thread safety of test factories.

Modified:
    commons/proper/pool/trunk/src/test/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java

Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java?rev=1173922&r1=1173921&r2=1173922&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/test/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
Thu Sep 22 01:10:13 2011
@@ -27,9 +27,10 @@ import static junit.framework.Assert.fai
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.NoSuchElementException;
 import java.util.Random;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.commons.pool2.KeyedObjectPool;
 import org.apache.commons.pool2.KeyedPoolableObjectFactory;
@@ -53,14 +54,16 @@ public class TestGenericKeyedObjectPool 
 
         KeyedPoolableObjectFactory<Object,Object> factory =
                 new KeyedPoolableObjectFactory<Object,Object>()  {
-            HashMap<Object,Integer> map = new HashMap<Object,Integer>();
+            ConcurrentHashMap<Object,AtomicInteger> map = new ConcurrentHashMap<Object,AtomicInteger>();
             public Object makeObject(Object key) {
                 int counter = 0;
-                Integer Counter = map.get(key);
+                AtomicInteger Counter = map.get(key);
                 if(null != Counter) {
-                    counter = Counter.intValue();
+                    counter = Counter.incrementAndGet();
+                } else {
+                    map.put(key, new AtomicInteger(0));
+                    counter = 0;
                 }
-                map.put(key,new Integer(counter + 1));
                 return String.valueOf(key) + String.valueOf(counter);
             }
             public void destroyObject(Object key, Object obj) { }
@@ -1449,14 +1452,16 @@ public class TestGenericKeyedObjectPool 
             this.valid = valid;
         }
         public String makeObject(K key) {
+            String out = null;
             synchronized(this) {
                 activeCount++;
                 if (activeCount > maxTotalPerKey) {
                     throw new IllegalStateException(
                         "Too many active instances: " + activeCount);
                 }
+                out = String.valueOf(key) + String.valueOf(counter++);
             }
-            return String.valueOf(key) + String.valueOf(counter++);
+            return out;
         }
         public void destroyObject(K key, String obj) throws Exception {
             doWait(destroyLatency);



Mime
View raw message