You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by hc...@apache.org on 2005/01/28 12:17:49 UTC
cvs commit: jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft SoftRefCacheCleaner.java SoftRefFileCache.java
hchar 2005/01/28 03:17:49
Modified: sandbox/yajcache/src/org/apache/jcs/yajcache/file
CacheFileDAO.java
sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref
KeyedRefCollector.java
sandbox/yajcache/src/org/apache/jcs/yajcache/soft
SoftRefCacheCleaner.java SoftRefFileCache.java
Log:
replace volatile int with AtomicInteger
Revision Changes Path
1.4 +16 -15 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/file/CacheFileDAO.java
Index: CacheFileDAO.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/file/CacheFileDAO.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- CacheFileDAO.java 27 Jan 2005 11:00:31 -0000 1.3
+++ CacheFileDAO.java 28 Jan 2005 11:17:49 -0000 1.4
@@ -20,6 +20,7 @@
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
+import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jcs.yajcache.lang.annotation.*;
@@ -36,13 +37,13 @@
public enum CacheFileDAO {
inst;
- private volatile int countWriteIOException;
- private volatile int countWriteCloseException;
- private volatile int countReadIOException;
- private volatile int countReadCloseException;
- private volatile int countCorruptMinLength;
- private volatile int countCorruptLength;
- private volatile int countCorruptInvalid;
+ private AtomicInteger countWriteIOException = new AtomicInteger(0);
+ private AtomicInteger countWriteCloseException = new AtomicInteger(0);
+ private AtomicInteger countReadIOException = new AtomicInteger(0);
+ private AtomicInteger countReadCloseException = new AtomicInteger(0);
+ private AtomicInteger countCorruptMinLength = new AtomicInteger(0);
+ private AtomicInteger countCorruptLength = new AtomicInteger(0);
+ private AtomicInteger countCorruptInvalid = new AtomicInteger(0);
private Log log = LogFactory.getLog(this.getClass());
@@ -64,14 +65,14 @@
CacheFileContent.getInstance(type, val).write(raf);
return true;
} catch(IOException ex) {
- countWriteIOException++;
+ countWriteIOException.incrementAndGet();
log.error("", ex);
} finally {
if (raf != null) {
try {
raf.close();
} catch(Exception ex) {
- countWriteCloseException++;
+ countWriteCloseException.incrementAndGet();
log.error("", ex);
}
}
@@ -94,7 +95,7 @@
final long fileSize = file.length();
if (fileSize <= CacheFileContent.MIN_FILE_LENGTH) {
- countCorruptMinLength++;
+ countCorruptMinLength.incrementAndGet();
log.warn("Corrupted file which failed the minimum length condition for cacheName="
+ cacheName + " key=" + key);
return CacheFileContent.CORRUPTED;
@@ -108,7 +109,7 @@
final int contentLength = (int)fileSize - CacheFileContent.MIN_FILE_LENGTH;
if (contentLength != cfc.getContentLength()) {
- countCorruptLength++;
+ countCorruptLength.incrementAndGet();
log.warn("Corrupted file with unexpected content length for cacheName="
+ cacheName + " key=" + key);
return CacheFileContent.CORRUPTED;
@@ -116,24 +117,24 @@
}
else
{
- countCorruptInvalid++;
+ countCorruptInvalid.incrementAndGet();
log.warn("Corrupted file for cacheName=" + cacheName
+ " key=" + key);
return CacheFileContent.CORRUPTED;
}
return cfc;
} catch(IOException ex) {
- countReadIOException++;
+ countReadIOException.incrementAndGet();
log.warn(ex.getClass().getName(), ex);
} catch(org.apache.commons.lang.SerializationException ex) {
- countReadIOException++;
+ countReadIOException.incrementAndGet();
log.warn(ex.getClass().getName(), ex);
} finally {
if (raf != null) {
try {
raf.close();
} catch(Exception ex) {
- countReadCloseException++;
+ countReadCloseException.incrementAndGet();
log.error("", ex);
}
}
1.3 +4 -2 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref/KeyedRefCollector.java
Index: KeyedRefCollector.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref/KeyedRefCollector.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- KeyedRefCollector.java 28 Jan 2005 10:41:10 -0000 1.2
+++ KeyedRefCollector.java 28 Jan 2005 11:17:49 -0000 1.3
@@ -19,6 +19,7 @@
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -33,7 +34,7 @@
private Log log = debug ? LogFactory.getLog(this.getClass()) : null;
private final @NonNullable ReferenceQueue q;
private final @NonNullable ConcurrentMap<K, ? extends IKey<K>> synMap;
- private volatile int count;
+ private final AtomicInteger count = new AtomicInteger(0);
public KeyedRefCollector(
@NonNullable ReferenceQueue<?> q,
@@ -50,9 +51,10 @@
// remove unused lock; may fail but that's fine.
synMap.remove(keyedRef.getKey(), ref);
// referent should have been cleared by GC.
+ this.count.incrementAndGet();
}
}
public int getCount() {
- return count;
+ return this.count.intValue();
}
}
1.5 +19 -18 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefCacheCleaner.java
Index: SoftRefCacheCleaner.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefCacheCleaner.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- SoftRefCacheCleaner.java 28 Jan 2005 10:41:10 -0000 1.4
+++ SoftRefCacheCleaner.java 28 Jan 2005 11:17:49 -0000 1.5
@@ -17,6 +17,7 @@
package org.apache.jcs.yajcache.soft;
import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -33,12 +34,12 @@
private static final boolean debug = true;
private Log log = debug ? LogFactory.getLog(this.getClass()) : null;
- private volatile int countTryKeyClean;
- private volatile int countRemovedByOthers;
- private volatile int countKeyCleaned;
- private volatile int countDataRace;
- private volatile int countDataRaceAndRemovedByOthers;
- private volatile int countBye;
+ private AtomicInteger countTryKeyClean = new AtomicInteger(0);
+ private AtomicInteger countRemovedByOthers = new AtomicInteger(0);
+ private AtomicInteger countKeyCleaned = new AtomicInteger(0);
+ private AtomicInteger countDataRace = new AtomicInteger(0);
+ private AtomicInteger countDataRaceAndRemovedByOthers = new AtomicInteger(0);
+ private AtomicInteger countBye = new AtomicInteger(0);
<V> void cleanupKey(@NonNullable Map<String,
KeyedSoftReference<String,V>> map, @NonNullable String key)
@@ -47,14 +48,14 @@
// already garbage collected. So try to clean up the key.
if (debug)
log.debug("Try to clean up the key");
- this.countTryKeyClean++;
+ this.countTryKeyClean.incrementAndGet();
KeyedSoftReference<String,V> oldRef = map.remove(key);
// If oldRef is null, the key has just been
// cleaned up by another thread.
if (oldRef == null) {
if (debug)
log.debug("Key has just been removed by another thread.");
- this.countRemovedByOthers++;
+ this.countRemovedByOthers.incrementAndGet();
return;
}
// Check for race condition.
@@ -65,14 +66,14 @@
oldRef.clear();
if (debug)
log.debug("Key removed and Soft Reference cleared.");
- this.countKeyCleaned++;
+ this.countKeyCleaned.incrementAndGet();
return;
}
// Race condition.
do {
if (debug)
log.debug("Race condition occurred. So put back the old stuff.");
- this.countDataRace++;
+ this.countDataRace.incrementAndGet();
// race condition occurred
// put back the old stuff
val = oldVal;
@@ -82,7 +83,7 @@
// key has just been cleaned up by another thread.
if (debug)
log.debug("Key has just been removed by another thread.");
- this.countDataRaceAndRemovedByOthers++;
+ this.countDataRaceAndRemovedByOthers.incrementAndGet();
return;
}
oldVal = oldRef.get();
@@ -90,31 +91,31 @@
if (debug)
log.debug("Bye.");
- this.countBye++;
+ this.countBye.incrementAndGet();
return;
}
public int getCountTryKeyClean() {
- return countTryKeyClean;
+ return countTryKeyClean.intValue();
}
public int getCountRemovedByOthers() {
- return countRemovedByOthers;
+ return countRemovedByOthers.intValue();
}
public int getCountKeyCleaned() {
- return countKeyCleaned;
+ return countKeyCleaned.intValue();
}
public int getCountDataRace() {
- return countDataRace;
+ return countDataRace.intValue();
}
public int getCountDataRaceAndRemovedByOthers() {
- return countDataRaceAndRemovedByOthers;
+ return countDataRaceAndRemovedByOthers.intValue();
}
public int getCountBye() {
- return countBye;
+ return countBye.intValue();
}
@Override public @NonNullable String toString() {
1.3 +7 -6 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefFileCache.java
Index: SoftRefFileCache.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefFileCache.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SoftRefFileCache.java 28 Jan 2005 10:41:10 -0000 1.2
+++ SoftRefFileCache.java 28 Jan 2005 11:17:49 -0000 1.3
@@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
@@ -71,13 +72,13 @@
private final @NonNullable CacheChangeSupport<V> cacheChangeSupport =
new CacheChangeSupport<V>(this);
- private volatile int countGet;
- private volatile int countGetHitMemory;
- private volatile int countGetHitFile;
- private volatile int countGetMiss;
+ private AtomicInteger countGet = new AtomicInteger(0);
+ private AtomicInteger countGetHitMemory = new AtomicInteger(0);
+ private AtomicInteger countGetHitFile = new AtomicInteger(0);
+ private AtomicInteger countGetMiss = new AtomicInteger(0);
- private volatile int countPut;
- private volatile int countRemove;
+ private AtomicInteger countPut = new AtomicInteger(0);
+ private AtomicInteger countRemove = new AtomicInteger(0);
public String getName() {
return this.name;
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org