hchar 2005/01/29 05:30:57
Modified:
sandbox/yajcache/src/org/apache/jcs/yajcache/util/concurrent/locks
KeyedReadWriteLock.java
Log:
delay lock instantiation + add counts + annotate
Revision Changes Path
1.5 +67 -21
jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/util/concurrent/locks/KeyedReadWriteLock.java
Index: KeyedReadWriteLock.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/util/concurrent/locks/KeyedReadWriteLock.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- KeyedReadWriteLock.java 29 Jan 2005 12:37:01 -0000 1.4
+++ KeyedReadWriteLock.java 29 Jan 2005 13:30:57 -0000 1.5
@@ -19,12 +19,16 @@
import java.lang.ref.ReferenceQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.jcs.yajcache.lang.ref.KeyedRefCollector;
import org.apache.jcs.yajcache.lang.ref.KeyedWeakReference;
+import org.apache.jcs.yajcache.lang.annotation.*;
+
/**
* Factory for key specific ReadWriteLock.
@@ -32,14 +36,27 @@
*
* @author Hanson Char
*/
[EMAIL PROTECTED]
public class KeyedReadWriteLock<K> implements IKeyedReadWriteLock<K> {
- private final ConcurrentMap<K, KeyedWeakReference<K,ReadWriteLock>>
rwlMap =
+ private static final boolean debug = true;
+
+ private final @NonNullable ConcurrentMap<K,
KeyedWeakReference<K,ReadWriteLock>> rwlMap =
new ConcurrentHashMap<K, KeyedWeakReference<K,ReadWriteLock>>();
- private final Class<? extends ReadWriteLock> rwlClass;
- private final ReferenceQueue<ReadWriteLock> refQ =
+ private final @NonNullable Class<? extends ReadWriteLock> rwlClass;
+ private final @NonNullable ReferenceQueue<ReadWriteLock> refQ =
new ReferenceQueue<ReadWriteLock>();
- private final KeyedRefCollector<K> collector =
+ private final @NonNullable KeyedRefCollector<K> collector =
new KeyedRefCollector<K>(refQ, rwlMap);
+
+ private final AtomicInteger countRWLockCreate = new AtomicInteger(0);
+ private final AtomicInteger countReadLock = new AtomicInteger(0);
+ private final AtomicInteger countWriteLock = new AtomicInteger(0);
+
+ private final AtomicInteger countKeyHit = new AtomicInteger(0);
+ private final AtomicInteger countLockRefEmpty = new AtomicInteger(0);
+ private final AtomicInteger countLockNew = new AtomicInteger(0);
+ private final AtomicInteger countLockExist = new AtomicInteger(0);
+
public KeyedReadWriteLock() {
this.rwlClass = ReentrantReadWriteLock.class;
}
@@ -47,46 +64,62 @@
this.rwlClass = rwlClass;
}
public Lock readLock(K key) {
+ if (debug)
+ this.countReadLock.incrementAndGet();
return this.readWriteLock(key).readLock();
}
public Lock writeLock(K key) {
+ if (debug)
+ this.countWriteLock.incrementAndGet();
return this.readWriteLock(key).writeLock();
}
private ReadWriteLock readWriteLock(K key) {
- ReadWriteLock newLock = null;
- try {
- newLock = rwlClass.newInstance();
- } catch(IllegalAccessException ex) {
- throw new RuntimeException(ex);
- } catch(InstantiationException ex) {
- throw new RuntimeException(ex);
- }
- return this.getLock(key, newLock);
- }
- private ReadWriteLock getLock(final K key, ReadWriteLock newLock)
- {
this.collector.run();
- ReadWriteLock prev = null;
KeyedWeakReference<K,ReadWriteLock> prevRef = this.rwlMap.get(key);
if (prevRef != null) {
// existing lock may exist
- prev = prevRef.get();
+ if (debug)
+ this.countKeyHit.incrementAndGet();
+ ReadWriteLock prev = prevRef.get();
if (prev != null) {
// existing lock
+ if (debug)
+ this.countLockExist.incrementAndGet();
return prev;
}
+ if (debug)
+ this.countLockRefEmpty.incrementAndGet();
// stale reference; doesn't matter if fail
this.rwlMap.remove(key, prevRef);
}
+ // Instantiate a new RW lock.
+ ReadWriteLock newLock = null;
+ try {
+ newLock = rwlClass.newInstance();
+ if (debug)
+ this.countRWLockCreate.incrementAndGet();
+ } catch(IllegalAccessException ex) {
+ throw new RuntimeException(ex);
+ } catch(InstantiationException ex) {
+ throw new RuntimeException(ex);
+ }
+ return this.getLock(key, newLock);
+ }
+ private ReadWriteLock getLock(final K key, ReadWriteLock newLock)
+ {
final KeyedWeakReference<K,ReadWriteLock> newLockRef =
new KeyedWeakReference<K,ReadWriteLock>(key, newLock, refQ);
+ ReadWriteLock prev = null;
+ KeyedWeakReference<K,ReadWriteLock> prevRef = null;
do {
prevRef = this.rwlMap.putIfAbsent(key, newLockRef);
if (prevRef == null) {
// succesfully deposited the new lock.
+ if (debug)
+ this.countLockNew.incrementAndGet();
return newLock;
}
// existing lock may exist
@@ -94,13 +127,26 @@
if (prev != null) {
// exist lock
+ if (debug)
+ this.countLockExist.incrementAndGet();
return prev;
}
// stale reference; doesn't matter if fail
+ if (debug)
+ this.countLockRefEmpty.incrementAndGet();
this.rwlMap.remove(key, prevRef);
} while(true);
}
-// public void removeUnusedLocks() {
-// this.collector.run();
-// }
+ @Override public String toString() {
+ return new ToStringBuilder(this)
+ .append("\n").append("countReadLock", this.countReadLock)
+ .append("\n").append("countWriteLock", this.countWriteLock)
+ .append("\n").append("countRWLockCreate", this.countRWLockCreate)
+ .append("\n").append("countKeyHit", this.countKeyHit)
+ .append("\n").append("countLockExist", this.countLockExist)
+ .append("\n").append("countLockNew", this.countLockNew)
+ .append("\n").append("countLockRefEmpty", this.countLockRefEmpty)
+ .append("\n").append("collector", this.collector)
+ .toString();
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]