class FilterManager.FilterCleaner {
public void run () {
while (running) {
// sort items from oldest to newest
// we delete the oldest filters
if (cache.size() > cacheCleanSize) {
// empty the temporary set
sortedFilterItems.clear();
synchronized (cache) {
sortedFilterItems.addAll(cache.entrySet());
Iterator it = sortedFilterItems.iterator();
int numToDelete = (int) ((cache.size() - cacheCleanSize) * 1.5);
int counter = 0;
// loop over the set and delete all of the cache entries not used in a while
while (it.hasNext() && counter++ < numToDelete) {
Map.Entry entry = (Map.Entry)it.next();
cache.remove(entry.getKey());
}
}
// empty the set so we don't tie up the memory
sortedFilterItems.clear();
}
// take a nap
try {
Thread.sleep(cleanSleepTime);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
}
}
}
|