com.objectwave.utility
Class Timer

java.lang.Object
  |
  +--com.objectwave.utility.Timer

public class Timer
extends java.lang.Object

This class is based upon an article in JavaWorld.

Version:
$Id: Timer.java,v 2.1 2002/07/31 15:55:23 dave_hoag Exp $
Author:
Dave Hoag

Nested Class Summary
static class Timer.Mode
          Description of the Class
static class Timer.Test
          Test case.
 
Field Summary
protected  java.util.Hashtable assocation
           
static Timer.Mode CONTINUOUS
          Description of the Field
static java.lang.String EXPIRED
          Description of the Field
static long FOREVER
          Description of the Field
static Timer.Mode KEEPTHREAD
          Description of the Field
protected  LockManager lockManager
           
static Timer.Mode RUNONCE
          Description of the Field
static java.lang.String STOPPED
          Description of the Field
 
Constructor Summary
Timer(java.awt.event.ActionListener l, int delay)
          Run the action listener on its own thread in delay amount of time.
Timer(int delay)
          Using this constructor is the same as Timer(delay, Timer.CONTINUOUS );
Timer(int delay, Timer.Mode type)
           
 
Method Summary
 void addActionListener(java.awt.event.ActionListener l)
          Add an action listener to be notified at every clock tick.
 void finalize()
           
 void flushQueue()
          Stop notifying the action listeners with actionEvents.
 int getDelay()
          The time between clock ticks.
 boolean isStopped()
           
 boolean pause(long timeout)
          Pause the current thread for at most timeout milliseconds.
 void removeActionListener(java.awt.event.ActionListener l)
           
 int runningQueueSize()
          An estimate of how many action listeners are have been notified, but have yet to execute.
 void setDelay(int val)
          The time between clock ticks.
 void setEnableNotifications(boolean b)
           
 void start()
          Start the timer ticking.
 void stop()
          Stop the timer from ticking.
 void waitForActionListeners()
          Wait for all of the action listeners.
 boolean waitForTimer()
          Go into an indefinite wait state until the clock timer ticks.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lockManager

protected LockManager lockManager

assocation

protected java.util.Hashtable assocation

CONTINUOUS

public static final Timer.Mode CONTINUOUS
Description of the Field


RUNONCE

public static final Timer.Mode RUNONCE
Description of the Field


KEEPTHREAD

public static final Timer.Mode KEEPTHREAD
Description of the Field


STOPPED

public static final java.lang.String STOPPED
Description of the Field

See Also:
Constant Field Values

EXPIRED

public static final java.lang.String EXPIRED
Description of the Field

See Also:
Constant Field Values

FOREVER

public static final long FOREVER
Description of the Field

See Also:
Constant Field Values
Constructor Detail

Timer

public Timer(int delay,
             Timer.Mode type)
Parameters:
delay - int The time between clock ticks.
type - Description of Parameter

Timer

public Timer(int delay)
Using this constructor is the same as Timer(delay, Timer.CONTINUOUS );

Parameters:
delay - Description of Parameter
See Also:
#Timer(int, Mode)

Timer

public Timer(java.awt.event.ActionListener l,
             int delay)
Run the action listener on its own thread in delay amount of time.

Parameters:
l - The action listener upon which to invoke the command.
delay - The time in milliseconds to wait.
Method Detail

setDelay

public void setDelay(int val)
The time between clock ticks.

Parameters:
val - The new Delay value

setEnableNotifications

public void setEnableNotifications(boolean b)
Parameters:
b - The new EnableNotifications value

isStopped

public boolean isStopped()
Returns:
The Stopped value

getDelay

public int getDelay()
The time between clock ticks.

Returns:
The Delay value

start

public void start()
Start the timer ticking. If a timer of a type other than RUNONCE is used, it is necessary to call stop() to free up resources.

See Also:
stop()

stop

public void stop()
Stop the timer from ticking. NOTE: This does not stop any running actionListeners. This method MUST be called to free up resources.


finalize

public void finalize()
Overrides:
finalize in class java.lang.Object

pause

public boolean pause(long timeout)
Pause the current thread for at most timeout milliseconds. Before the time is completed, the thread may be interrupted from the clock. If there is no clock, then this will return immediately.

Parameters:
timeout - Description of Parameter
Returns:
Description of the Returned Value

waitForTimer

public boolean waitForTimer()
Go into an indefinite wait state until the clock timer ticks. Return immediately if the clock is not actively running.

Returns:
Description of the Returned Value

addActionListener

public void addActionListener(java.awt.event.ActionListener l)
Add an action listener to be notified at every clock tick. The 'actionCommand' of the action event will either be 'expired' or 'stopped'. If it is 'expired', the clock is still running. If it is 'stopped', the clock has been stopped. The action listener should decide how to respond to the various states. Each ActionListener that has been added will be notified of each clock tick. If the action listeners take longer than 'delay' to execute, the ticks will be queued. An approximation of the queue size can be obtained via the 'runningQueueSize()' method. The 'flushQueue' method will cause all of those queued up to never execute.

Parameters:
l - The feature to be added to the ActionListener attribute
See Also:
flushQueue(), runningQueueSize()

removeActionListener

public void removeActionListener(java.awt.event.ActionListener l)
Parameters:
l - Description of Parameter

runningQueueSize

public int runningQueueSize()
An estimate of how many action listeners are have been notified, but have yet to execute.

Returns:
Description of the Returned Value

flushQueue

public void flushQueue()
Stop notifying the action listeners with actionEvents.


waitForActionListeners

public void waitForActionListeners()
Wait for all of the action listeners.