add usage comments, reformat

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@542583 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2007-05-29 16:33:23 +00:00
parent 1e6537cf5e
commit e864ab6cd8
1 changed files with 30 additions and 6 deletions

View File

@ -19,17 +19,41 @@ package org.apache.solr.util;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /** Keep track of a reference count on a resource and close it when
* the count hits zero.
*
* By itself, this class could have some race conditions
* since there is no synchronization between the refcount
* check and the close. Solr's use in reference counting searchers
* is safe since the count can only hit zero if it's unregistered (and
* hence incref() will not be called again on it).
*
* @author yonik * @author yonik
* @version $Id$ * @version $Id$
*/ */
public abstract class RefCounted<Type> { public abstract class RefCounted<Type> {
protected final Type resource; protected final Type resource;
protected final AtomicInteger refcount= new AtomicInteger(); protected final AtomicInteger refcount = new AtomicInteger();
public RefCounted(Type resource) { this.resource = resource; }
public final RefCounted<Type> incref() { refcount.incrementAndGet(); return this; } public RefCounted(Type resource) {
public final Type get() { return resource; } this.resource = resource;
public void decref() { if (refcount.decrementAndGet()==0) close(); } }
public final RefCounted<Type> incref() {
refcount.incrementAndGet();
return this;
}
public final Type get() {
return resource;
}
public void decref() {
if (refcount.decrementAndGet() == 0) {
close();
}
}
protected abstract void close(); protected abstract void close();
} }