commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sandy...@apache.org
Subject svn commit: r383476 - /jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java
Date Mon, 06 Mar 2006 06:30:04 GMT
Author: sandymac
Date: Sun Mar  5 22:30:02 2006
New Revision: 383476

URL: http://svn.apache.org/viewcvs?rev=383476&view=rev
Log:
Improved behaior of SoftReferenceObjectPool while trying to fix related unit tests. Changes
will help getNumIdle() be more accurate with 
minimal overhead.

Modified:
    jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java

Modified: jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java?rev=383476&r1=383475&r2=383476&view=diff
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java
(original)
+++ jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java
Sun Mar  5 22:30:02 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-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.
@@ -17,6 +17,8 @@
 package org.apache.commons.pool.impl;
 
 import java.lang.ref.SoftReference;
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.Reference;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -31,6 +33,7 @@
  * {@link ObjectPool}.
  *
  * @author Rodney Waldhoff
+ * @author Sandy McArthur
  * @version $Revision$ $Date$
  */
 public class SoftReferenceObjectPool extends BaseObjectPool implements ObjectPool {
@@ -51,7 +54,7 @@
             for(int i=0;i<initSize;i++) {
                 Object obj = _factory.makeObject();
                 _factory.passivateObject(obj);
-                _pool.add(new SoftReference(obj));
+                _pool.add(new SoftReference(obj, refQueue));
             }
         }
     }
@@ -69,6 +72,7 @@
             } else {
                 SoftReference ref = (SoftReference)(_pool.remove(_pool.size() - 1));
                 obj = ref.get();
+                ref.clear(); // prevent this ref from being enqueued with refQueue.
             }
             if(null != _factory && null != obj) {
                 _factory.activateObject(obj);
@@ -98,7 +102,7 @@
         boolean shouldDestroy = !success;
         _numActive--;
         if(success) {
-            _pool.add(new SoftReference(obj));
+            _pool.add(new SoftReference(obj, refQueue));
         }
         notifyAll(); // _numActive has changed
 
@@ -133,6 +137,7 @@
     /** Returns an approximation not less than the of the number of idle instances in the
pool. */
     public synchronized int getNumIdle() {
         assertOpen();
+        pruneClearedReferences();
         return _pool.size();
     }
 
@@ -157,6 +162,7 @@
             }
         }
         _pool.clear();
+        pruneClearedReferences();
     }
 
     public synchronized void close() throws Exception {
@@ -176,11 +182,33 @@
         }
     }
 
+    /**
+     * If any idle objects were garabage collected, remove their
+     * {@link Reference} wrappers from the idle object pool.
+     */
+    private void pruneClearedReferences() {
+        Reference ref;
+        while ((ref = refQueue.poll()) != null) {
+            try {
+                _pool.remove(ref);
+            } catch (UnsupportedOperationException uoe) {
+                // ignored
+            }
+        }
+    }
+
     /** My pool. */
     private List _pool = null;
 
     /** My {@link PoolableObjectFactory}. */
     private PoolableObjectFactory _factory = null;
+
+    /**
+     * Queue of broken references that might be able to be removed from <code>_pool</code>.
+     * This is used to help {@link #getNumIdle()} be more accurate with minimial
+     * performance overhead.
+     */
+    private final ReferenceQueue refQueue = new ReferenceQueue();
 
     /** Number of active objects. */
     private int _numActive = 0;



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message