Author: markt
Date: Tue Dec 13 15:18:11 2011
New Revision: 1213732
URL: http://svn.apache.org/viewvc?rev=1213732&view=rev
Log:
Fix POOL-196. Avoid threading issues for getActiveTimeMillis()
Modified:
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/PooledObject.java
Modified:
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/PooledObject.java
URL:
http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/PooledObject.java?rev=1213732&r1=1213731&r2=1213732&view=diff
==============================================================================
---
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/PooledObject.java
(original)
+++
commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/PooledObject.java
Tue Dec 13 15:18:11 2011
@@ -26,8 +26,8 @@ public class PooledObject<T> implements
private T object = null;
private volatile PooledObjectState state = PooledObjectState.IDLE; //
@GuardedBy("this") to ensure transitions are valid
private final long createTime = System.currentTimeMillis();
- private long lastBorrowTime = createTime;
- private long lastReturnTime = createTime;
+ private volatile long lastBorrowTime = createTime;
+ private volatile long lastReturnTime = createTime;
public PooledObject(T object) {
this.object = object;
@@ -50,15 +50,19 @@ public class PooledObject<T> implements
}
/**
- * Obtain the time in milliseconds that this object last spend in the the
+ * Obtain the time in milliseconds that this object last spent in the the
* active state (it may still be active in which case subsequent calls will
* return an increased value).
*/
public long getActiveTimeMillis() {
- if (lastReturnTime > lastBorrowTime) {
- return lastReturnTime - lastBorrowTime;
+ // Take copies to avoid threading issues
+ long rTime = lastReturnTime;
+ long bTime = lastBorrowTime;
+
+ if (rTime > bTime) {
+ return rTime - bTime;
} else {
- return System.currentTimeMillis() - lastBorrowTime;
+ return System.currentTimeMillis() - bTime;
}
}