org.apache.tapestry.util
Class JanitorThread

java.lang.Object
  extended by java.lang.Thread
      extended by org.apache.tapestry.util.JanitorThread
All Implemented Interfaces:
Runnable

public class JanitorThread
extends Thread

A basic kind of janitor, an object that periodically invokes ICleanable.executeCleanup() on a set of objects.

The JanitorThread holds a weak reference to the objects it operates on.

Since:
1.0.5
Version:
$Id: JanitorThread.java 244162 2005-03-17 05:17:29Z pferraro $
Author:
Howard Lewis Ship

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
static long DEFAULT_INTERVAL_MILLIS
          Default number of seconds between janitor runs, about 30 seconds.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
JanitorThread()
          Creates a new daemon Janitor.
JanitorThread(String name)
          Creates new Janitor with the given name.
 
Method Summary
 void add(ICleanable cleanable)
          Adds a new cleanable object to the list of references.
 long getInterval()
           
static JanitorThread getSharedJanitorThread()
          Returns a shared instance of JanitorThread.
 void run()
          Alternates between waitForNextPass()and sweep().
 void setInterval(long value)
          Updates the property, which may not take effect until the next time the thread finishes sleeping.
protected  void sweep()
          Runs through the list of targets and invokes ICleanable.executeCleanup()on each of them.
 String toString()
           
protected  void waitForNextPass()
          Waits for the next run, by sleeping for the desired period.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_INTERVAL_MILLIS

public static final long DEFAULT_INTERVAL_MILLIS
Default number of seconds between janitor runs, about 30 seconds.

See Also:
Constant Field Values
Constructor Detail

JanitorThread

public JanitorThread()
Creates a new daemon Janitor.


JanitorThread

public JanitorThread(String name)
Creates new Janitor with the given name. The thread will have minimum priority and be a daemon.

Method Detail

getSharedJanitorThread

public static JanitorThread getSharedJanitorThread()
Returns a shared instance of JanitorThread. In most cases, the shared instance should be used, rather than creating a new instance; the exception being when particular scheduling is of concern. It is also bad policy to change the sleep interval on the shared janitor (though nothing prevents this, either).


getInterval

public long getInterval()

setInterval

public void setInterval(long value)
Updates the property, which may not take effect until the next time the thread finishes sleeping.

Parameters:
value - the interval, in milliseconds, between sweeps.
Throws:
IllegalStateException - always, if the receiver is the shared JanitorThread
IllegalArgumentException - if value is less than 1

add

public void add(ICleanable cleanable)
Adds a new cleanable object to the list of references. Care should be taken that objects are not added multiple times; they will be cleaned too often.


sweep

protected void sweep()
Runs through the list of targets and invokes ICleanable.executeCleanup()on each of them. WeakReferences that have been invalidated are weeded out.


waitForNextPass

protected void waitForNextPass()
Waits for the next run, by sleeping for the desired period. Returns true if the sleep was successful, or false if the thread was interrupted (and should shut down).


run

public void run()
Alternates between waitForNextPass()and sweep().

Specified by:
run in interface Runnable
Overrides:
run in class Thread

toString

public String toString()
Overrides:
toString in class Thread