org.apache.tapestry.util
Class JanitorThread

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--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,v 1.4 2004/01/19 21:32:17 hlship Exp $
Author:
Howard Lewis Ship

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, then interrupts the thread.
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, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, 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, then interrupts the thread.

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.


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