Author: markt
Date: Wed Dec 14 22:47:17 2011
New Revision: 1214517

URL: http://svn.apache.org/viewvc?rev=1214517&view=rev
Log:
Fix POOL-159. Expose (an estimate of) the number of threads blocked waiting for 
an object from the pool.

Modified:
    
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
    
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java

Modified: 
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
URL: 
http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java?rev=1214517&r1=1214516&r2=1214517&view=diff
==============================================================================
--- 
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
 (original)
+++ 
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
 Wed Dec 14 22:47:17 2011
@@ -1816,6 +1816,50 @@ public class GenericKeyedObjectPool<K,T>
         return maxBorrowWaitTimeMillis;
     }
 
+    /**
+     * Return an estimate of the number of threads currently blocked waiting 
for
+     * an object from the pool. This is intended for monitoring only, not for
+     * synchronization control.
+     * 
+     * @return  An estimate of the number of threads currently blocked waiting
+     *          for an object from the pool 
+     */
+    public int getNumWaiters() {
+        int result = 0;
+        
+        if (getBlockWhenExhausted()) {
+            Iterator<ObjectDeque<T>> iter = poolMap.values().iterator();
+            
+            while (iter.hasNext()) {
+                // Assume no overflow
+                result += iter.next().getIdleObjects().getTakeQueueLength();
+            }
+        }
+        
+        return result;
+    }
+    
+    /**
+     * Return an estimate of the number of threads currently blocked waiting 
for
+     * an object from the pool for the given key. This is intended for
+     * monitoring only, not for synchronization control.
+     * 
+     * @return  An estimate of the number of threads currently blocked waiting
+     *          for an object from the pool for the given key
+     */
+    public int getNumWaiters(K key) {
+        if (getBlockWhenExhausted()) {
+            final ObjectDeque<T> objectDeque = poolMap.get(key);
+            if (objectDeque == null) {
+                return 0;
+            } else {
+                return objectDeque.getIdleObjects().getTakeQueueLength();
+            }
+        } else {
+            return 0;
+        }
+    }
+
     //--- inner classes ----------------------------------------------
 
     /**

Modified: 
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java
URL: 
http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java?rev=1214517&r1=1214516&r2=1214517&view=diff
==============================================================================
--- 
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java
 (original)
+++ 
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java
 Wed Dec 14 22:47:17 2011
@@ -1341,6 +1341,22 @@ public class GenericObjectPool<T> extend
         return maxBorrowWaitTimeMillis;
     }
 
+    /**
+     * Return an estimate of the number of threads currently blocked waiting 
for
+     * an object from the pool. This is intended for monitoring only, not for
+     * synchronization control.
+     * 
+     * @return  An estimate of the number of threads currently blocked waiting
+     *          for an object from the pool 
+     */
+    public int getNumWaiters() {
+        if (getBlockWhenExhausted()) {
+            return idleObjects.getTakeQueueLength();
+        } else {
+            return 0;
+        }
+    }
+
     // --- inner classes ----------------------------------------------
 
     /**


Reply via email to