SmartFrog 3.10.000

org.smartfrog.sfcore.common
Class Timer

java.lang.Object
  extended by org.smartfrog.sfcore.common.Timer
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
LivenessSender

public class Timer
extends java.lang.Object
implements java.lang.Runnable

Implements a simple timer. Useful classes subclass from this and override the timerTick() method to provide functional behaviour on every tick.


Field Summary
protected  java.lang.String name
          Thread name.
protected  boolean stopped
          Flag indicating whether this timer has been stopped.
protected  long tickDelay
          Delay between ticks in milliseconds.
protected  java.lang.Thread timerThread
          Holder for thread.
protected  boolean waiting
          Flag indicating that thread is currently waiting.
 
Constructor Summary
Timer(long delay)
          Constructor.
 
Method Summary
protected  java.lang.Thread createThread(java.lang.Runnable run)
          Creates a thread on given runnable interface.
protected  void doTick()
          Does the tick.
 boolean getStopped()
          Returns the stopped flag.
 long getTickDelay()
          Gets the tick delay for this timer.
 boolean getWaiting()
          Returns the waiting flag.
 void reschedule()
          Reschedules the timer thread.
 void run()
          Thread entry point.
 void setName(java.lang.String name)
          Sets the name
 void setTickDelay(long delay)
          Sets the tick delay, reschedules the thread if currently waiting.
 void start()
          Starts the timer off (ie starts the timer thread).
 void stop()
          Stops the thread indirectly by setting the stopped flag.
protected  void timerTick()
          Subclasses should implement this message.
protected  void timerWait()
          Does the actual wait.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tickDelay

protected long tickDelay
Delay between ticks in milliseconds.


timerThread

protected java.lang.Thread timerThread
Holder for thread.


stopped

protected boolean stopped
Flag indicating whether this timer has been stopped.


waiting

protected boolean waiting
Flag indicating that thread is currently waiting.


name

protected java.lang.String name
Thread name.

Constructor Detail

Timer

public Timer(long delay)
Constructor.

Parameters:
delay - delay between ticks
Method Detail

start

public void start()
Starts the timer off (ie starts the timer thread).


stop

public void stop()
Stops the thread indirectly by setting the stopped flag. This prevents condition where terminating timer thread terminates the thread calling stop since they are the same!


reschedule

public void reschedule()
Reschedules the timer thread. This is only done if the timer thread is currently waiting.


getStopped

public boolean getStopped()
Returns the stopped flag.

Returns:
true if timer stopped, false if running

getWaiting

public boolean getWaiting()
Returns the waiting flag.

Returns:
true if currently waiting, false if not

getTickDelay

public long getTickDelay()
Gets the tick delay for this timer.

Returns:
tick delay for timer
See Also:
setTickDelay(long)

setTickDelay

public void setTickDelay(long delay)
Sets the tick delay, reschedules the thread if currently waiting.

Parameters:
delay - new tick delay
See Also:
getTickDelay()

timerWait

protected void timerWait()
Does the actual wait.


doTick

protected void doTick()
Does the tick. Will call timertick, but does the stopped check first. Ignores any errors during timertick.


createThread

protected java.lang.Thread createThread(java.lang.Runnable run)
Creates a thread on given runnable interface. Default just creaates a new thread. Subclasses can override to, for example put thread in a particular thread group.

Parameters:
run - runnable object
Returns:
thread on given interface

timerTick

protected void timerTick()
Subclasses should implement this message.


run

public void run()
Thread entry point.

Specified by:
run in interface java.lang.Runnable

setName

public void setName(java.lang.String name)
Sets the name

Parameters:
name - String name

SmartFrog CORE 3.10.000

(C) Copyright 1998-2006 Hewlett-Packard Development Company, LP