au.com.lastweekend.jen.util
Class LoopRunnable

java.lang.Object
  extended by au.com.lastweekend.jen.util.LoopRunnable
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
AbstractWatchdog

public abstract class LoopRunnable
extends java.lang.Object
implements java.lang.Runnable

Once started repeatedly runs a loop until stopped.

Version:
$Id$
Author:
Grant Gardner grant@lastweekend.com.au

Constructor Summary
LoopRunnable()
           
 
Method Summary
protected abstract  void loop()
          This method will run repeatedly until the stop method is called or the method throws an exception.
 void run()
           
 java.lang.Thread start(java.lang.String name)
          Convenience method to start this runnable in a new thread.
 void stop()
          Stops the loop.
 void stopAtShutdown()
          Adds a shutdown hook to stop this process when the JVM shuts down.
protected  void stopped()
          Called when the loop exits.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LoopRunnable

public LoopRunnable()
Method Detail

stopped

protected void stopped()
Called when the loop exits.


loop

protected abstract void loop()
This method will run repeatedly until the stop method is called or the method throws an exception. The stop process works by interrupting the thread. If you want to escape your loop early check Thread.currentThread().isInterrupted() Wrapping some part of the loop in synchronized(this) {...} will block the stopping thread.


run

public final void run()
Specified by:
run in interface java.lang.Runnable

stop

public final void stop()
Stops the loop.


start

public java.lang.Thread start(java.lang.String name)
Convenience method to start this runnable in a new thread.

Parameters:
name -
Returns:
a new, started thread.

stopAtShutdown

public final void stopAtShutdown()
Adds a shutdown hook to stop this process when the JVM shuts down.



Copyright © 2008. All Rights Reserved.