Title: [524] trunk/rails-integration/src/main/java/org/jruby/webapp/util/CustomObjectPool.java: Just noticed AtomicInteger, which can remove the rest of the locking
Revision
524
Author
tantalon
Date
2007-04-30 20:47:48 -0400 (Mon, 30 Apr 2007)

Log Message

Just noticed AtomicInteger, which can remove the rest of the locking

Modified Paths


Diff

Modified: trunk/rails-integration/src/main/java/org/jruby/webapp/util/CustomObjectPool.java (523 => 524)


--- trunk/rails-integration/src/main/java/org/jruby/webapp/util/CustomObjectPool.java	2007-04-30 13:56:55 UTC (rev 523)
+++ trunk/rails-integration/src/main/java/org/jruby/webapp/util/CustomObjectPool.java	2007-05-01 00:47:48 UTC (rev 524)
@@ -4,6 +4,7 @@
 import edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue;
 import edu.emory.mathcs.backport.java.util.concurrent.ArrayBlockingQueue;
 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
+import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
 import java.util.NoSuchElementException;
 /**
  * An object pool which assumes that object creation is slow, and should be done in a separate thread.
@@ -18,8 +19,8 @@
 	private BlockingQueue pool;
 	private int maxObjects;
 	private int minIdle;
-	private int numObjects;
-	private int numActive;
+	private AtomicInteger numObjects;
+	private AtomicInteger numActive;
 	private boolean open;
 	private int checkInterval;
 
@@ -36,6 +37,8 @@
 		this.maxObjects = maxObjects;
 		this.factory = factory;
 		this.pool = new ArrayBlockingQueue(maxObjects);
+		this.numObjects = new AtomicInteger();
+		this.numActive = new AtomicInteger();
 
 		if (checkInterval != 0) {
 			new Thread(new PoolSizeManager(), "ObjectPoolManager").start();
@@ -45,19 +48,13 @@
 	public Object borrowObject() throws InterruptedException {
 		Object object = pool.poll(maxWait, TimeUnit.MILLISECONDS);
 		if (object == null) throw new NoSuchElementException("Time out waiting for object");
-
-		synchronized (this) {
-			numActive++;
-		}
+		numActive.incrementAndGet();
 		return object;
 	}
 
 	public void returnObject(Object obj) throws Exception {
 		if (open && pool.offer(obj)) {
-			// added ok
-			synchronized (this) {
-				numActive--;
-			}
+			numActive.decrementAndGet();
 		} else {
 			invalidateObject(obj);
 		}
@@ -67,20 +64,18 @@
 		try {
 			factory.destroyObject(obj);
 		} finally {
-			synchronized (this) {
-				numActive--;
-				numObjects--;
-			}
+			numActive.decrementAndGet();
+			numObjects.decrementAndGet();
 		}
 	}
 
 	public synchronized void addObject() throws Exception {
 		// enforce the constraints, so this only happens if it makes sense to
-		if (numObjects < maxObjects && getNumIdle() < minIdle) {
+		if (getNumObjects() < maxObjects && getNumIdle() < minIdle) {
 			Object object = factory.makeObject();
 			try {
 				pool.add(object);
-				numObjects++;
+				numObjects.incrementAndGet();
 			} catch (IllegalStateException e) {
 				factory.destroyObject(object);
 				throw e;
@@ -94,14 +89,18 @@
 		}
 	}
 
-	public synchronized int getNumIdle() {
-		return numObjects - numActive;
+	public int getNumIdle() {
+		return getNumObjects() - getNumActive();
 	}
 
-	public synchronized int getNumActive() {
-		return numActive;
+	public int getNumActive() {
+		return numActive.get();
 	}
 
+	public int getNumObjects() {
+		return numObjects.get();
+	}
+
 	public int getMinIdle() {
 		return minIdle;
 	}
@@ -114,10 +113,6 @@
 		this.maxWait = maxWait;
 	}
 
-	public int getNumObjects() {
-		return numObjects;
-	}
-
 	public void clear() {
 		throw new UnsupportedOperationException();
 	}
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to