[jboss-cvs] JBossAS SVN: r107587 - in projects/ejb3/branches/jboss-ejb3-core-1.3/src: test/java/org/jboss/ejb3/test/threadlocal/unit and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Aug 13 07:15:08 EDT 2010


Author: wolfc
Date: 2010-08-13 07:15:08 -0400 (Fri, 13 Aug 2010)
New Revision: 107587

Modified:
   projects/ejb3/branches/jboss-ejb3-core-1.3/src/main/java/org/jboss/ejb3/pool/ThreadlocalPool.java
   projects/ejb3/branches/jboss-ejb3-core-1.3/src/test/java/org/jboss/ejb3/test/threadlocal/unit/ThreadLocalPoolUnitTestCase.java
Log:
EJBTHREE-2147: use an atomic instead of synchronized block


Modified: projects/ejb3/branches/jboss-ejb3-core-1.3/src/main/java/org/jboss/ejb3/pool/ThreadlocalPool.java
===================================================================
--- projects/ejb3/branches/jboss-ejb3-core-1.3/src/main/java/org/jboss/ejb3/pool/ThreadlocalPool.java	2010-08-13 10:27:11 UTC (rev 107586)
+++ projects/ejb3/branches/jboss-ejb3-core-1.3/src/main/java/org/jboss/ejb3/pool/ThreadlocalPool.java	2010-08-13 11:15:08 UTC (rev 107587)
@@ -28,7 +28,9 @@
 import org.jboss.lang.ref.WeakThreadLocal;
 import org.jboss.logging.Logger;
 
+import java.util.concurrent.atomic.AtomicInteger;
 
+
 /**
  * Pools EJBs within a ThreadLocal.
  *
@@ -41,7 +43,7 @@
    
    protected Pool pool = new InfinitePool();
    protected WeakThreadLocal<BeanContext> currentBeanContext = new WeakThreadLocal<BeanContext>();
-   private int inUse = 0;
+   private AtomicInteger inUse = new AtomicInteger();
    
    public ThreadlocalPool()
    {
@@ -60,7 +62,7 @@
    public void discard(BeanContext obj)
    {
       pool.discard(obj);
-      --inUse;
+      inUse.decrementAndGet();
    }
    
    public void destroy()
@@ -72,47 +74,32 @@
       // This really serves little purpose, because we want the whole thread local map to die
       currentBeanContext.remove();
       
-      inUse = 0;
+      inUse.getAndSet(0);
    }
    
    public BeanContext get()
    {
-      BeanContext ctx = null;
-      
-      synchronized(pool)
-      {
-         ctx = currentBeanContext.get();
-         if (ctx != null)
-         {
-            currentBeanContext.set(null);
-            ++inUse;
-            return ctx;
-         }
-   
+      BeanContext ctx = currentBeanContext.get();
+      if (ctx != null)
+         currentBeanContext.set(null);
+      else
          ctx = create();
-         ++inUse;
-      }
+
+      inUse.incrementAndGet();
       
       return ctx;
    }
 
    public BeanContext get(Class[] initTypes, Object[] initValues)
    {
-      BeanContext ctx = null;
-      synchronized(pool)
-      {
-         ctx = currentBeanContext.get();
-         if (ctx != null)
-         {
-            currentBeanContext.set(null);
-            ++inUse;
-            return ctx;
-         }
-   
+      BeanContext ctx = currentBeanContext.get();
+      if (ctx != null)
+         currentBeanContext.set(null);
+      else
          ctx = create(initTypes, initValues);
-         ++inUse;
-      }
-      
+
+      inUse.incrementAndGet();
+
       return ctx;
    }
 
@@ -123,19 +110,12 @@
    
    public void release(BeanContext ctx)
    {
-      synchronized(pool)
-      {
-         if (currentBeanContext.get() != null)
-         {
-            remove(ctx);
-         }
-         else
-         {
-            currentBeanContext.set(ctx);
-         }
-         
-         --inUse;
-      }
+      if (currentBeanContext.get() != null)
+         remove(ctx);
+      else
+         currentBeanContext.set(ctx);
+
+      inUse.decrementAndGet();
    }
    
    public void remove(BeanContext ctx)
@@ -155,7 +135,7 @@
    
    public int getAvailableCount()
    {
-      return getMaxSize() - inUse;
+      return getMaxSize() - inUse.get();
    }
    
    public int getCreateCount()

Modified: projects/ejb3/branches/jboss-ejb3-core-1.3/src/test/java/org/jboss/ejb3/test/threadlocal/unit/ThreadLocalPoolUnitTestCase.java
===================================================================
--- projects/ejb3/branches/jboss-ejb3-core-1.3/src/test/java/org/jboss/ejb3/test/threadlocal/unit/ThreadLocalPoolUnitTestCase.java	2010-08-13 10:27:11 UTC (rev 107586)
+++ projects/ejb3/branches/jboss-ejb3-core-1.3/src/test/java/org/jboss/ejb3/test/threadlocal/unit/ThreadLocalPoolUnitTestCase.java	2010-08-13 11:15:08 UTC (rev 107587)
@@ -78,7 +78,31 @@
       assertEquals(1, pool.getRemoveCount());
       assertEquals(1, MockBean.finalizers);
    }
-   
+
+   public void testInUse1()
+   {
+      ThreadlocalPool pool = new ThreadlocalPool();
+      Container container = new MockContainer();
+      int maxSize = -1;
+      int timeout = -1;
+      pool.initialize(container, maxSize, timeout);
+
+      assertEquals(0, pool.getAvailableCount());
+      
+      BeanContext ctx = pool.get();
+
+      assertEquals(0, pool.getAvailableCount());
+
+      pool.release(ctx);
+      ctx = null;
+
+      assertEquals(1, pool.getAvailableCount());
+
+      pool.destroy();
+
+      gc();
+   }
+
    public void testWithThreads() throws Exception
    {
       final ThreadlocalPool pool = new ThreadlocalPool();



More information about the jboss-cvs-commits mailing list