org.apache.jcs.engine.memory.shrinking
Class ShrinkerThread

java.lang.Object
  extended byjava.lang.Thread
      extended byorg.apache.jcs.engine.memory.shrinking.ShrinkerThread
All Implemented Interfaces:
java.lang.Runnable

public class ShrinkerThread
extends java.lang.Thread

A background memory shrinker. Memory problems and concurrent modification exception caused by acting directly on an iterator of the underlying memory cache should have been solved.

Version:
$Id: ShrinkerThread.java,v 1.10 2003/08/22 11:57:16 mpoeschl Exp $
Author:
Aaron Smuts

Field Summary
(package private)  boolean alive
          Flag that indicates if the thread is still alive
private  MemoryCache cache
          The MemoryCache instance which this shrinker is watching
private static org.apache.commons.logging.Log log
           
private  long maxMemoryIdleTime
          Maximum memory idle time for the whole cache
private  long shrinkerInterval
          The time to sleep between shrink runs
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ShrinkerThread(MemoryCache cache)
          Constructor for the ShrinkerThread object.
 
Method Summary
private  boolean checkForRemoval(ICacheElement cacheElement, long now)
          Check if either lifetime or idletime has expired for the provided event, and remove it from the cache if so.
private  void handleElementEvents(ICacheElement cacheElement, int eventType)
          Handle any events registered for the given element of the given event type.
 void kill()
          Graceful shutdown after this round of processing.
 void run()
          Main processing method for the ShrinkerThread object
protected  void shrink()
          This method is called when the thread wakes up.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

private static final org.apache.commons.logging.Log log

cache

private final MemoryCache cache
The MemoryCache instance which this shrinker is watching


shrinkerInterval

private final long shrinkerInterval
The time to sleep between shrink runs


maxMemoryIdleTime

private final long maxMemoryIdleTime
Maximum memory idle time for the whole cache


alive

boolean alive
Flag that indicates if the thread is still alive

Constructor Detail

ShrinkerThread

public ShrinkerThread(MemoryCache cache)
Constructor for the ShrinkerThread object.

Parameters:
cache - The MemoryCache which the new shrinker should watch.
Method Detail

kill

public void kill()
Graceful shutdown after this round of processing.


run

public void run()
Main processing method for the ShrinkerThread object


shrink

protected void shrink()
This method is called when the thread wakes up. Frist the method obtains an array of keys for the cache region. It iterates through the keys and tries to get the item from the cache without affecting the last access or position of the item. The item is checked for expiration, the expiration check has 3 parts:
  1. Has the cacheattributes.MaxMemoryIdleTimeSeconds defined for the region been exceeded? If so, the item should be move to disk.
  2. Has the item exceeded MaxLifeSeconds defined in the element attributes? If so, remove it.
  3. Has the item exceeded IdleTime defined in the element atributes? If so, remove it. If there are event listeners registered for the cache element, they will be called.


checkForRemoval

private boolean checkForRemoval(ICacheElement cacheElement,
                                long now)
                         throws java.io.IOException
Check if either lifetime or idletime has expired for the provided event, and remove it from the cache if so.

Parameters:
cacheElement - Element to check for expiration
now - Time to consider expirations relative to
Returns:
true if the element should be removed, or false.
Throws:
java.io.IOException

handleElementEvents

private void handleElementEvents(ICacheElement cacheElement,
                                 int eventType)
                          throws java.io.IOException
Handle any events registered for the given element of the given event type.

Parameters:
cacheElement - Element to handle events for
eventType - Type of event to handle
Throws:
java.io.IOException - If an error occurs