@SuppressWarnings("unchecked") public void cleanup() { long now = System.currentTimeMillis(); ArrayList<K> keysToDelete = null; synchronized (cacheMap) { MapIterator itr = cacheMap.mapIterator(); keysToDelete = new ArrayList<K>((cacheMap.size() / 2) + 1); K key = null; CachedObject c = null; while (itr.hasNext()) { key = (K) itr.next(); c = (CachedObject) itr.getValue(); if (c != null && (now > ((1000L * timeToLiveInSeconds) + c.lastAccessed))) { keysToDelete.add(key); } } } for (K key : keysToDelete) { synchronized (cacheMap) { cacheMap.remove(key); } Thread.yield(); } }
/** Write the map out using a custom routine. */ private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(size()); for (MapIterator it = mapIterator(); it.hasNext(); ) { out.writeObject(it.next()); // key out.writeObject(it.getValue()); // value } }