com.caucho.util
Class Alarm

java.lang.Object
  extended by com.caucho.util.Alarm
All Implemented Interfaces:
ThreadTask, java.lang.Runnable
Direct Known Subclasses:
WeakAlarm

public class Alarm
extends java.lang.Object
implements ThreadTask

The alarm class provides a lightweight event scheduler. This allows an objects to schedule a timeout without creating a new thread.

A separate thread periodically tests the queue for alarms ready.


Constructor Summary
protected Alarm()
          Create a new wakeup alarm with a designated listener as a callback.
  Alarm(AlarmListener listener)
          Create a new wakeup alarm with a designated listener as a callback.
  Alarm(AlarmListener listener, long delta)
          Creates a new alarm and schedules its wakeup.
  Alarm(java.lang.String name, AlarmListener listener)
          Create a new wakeup alarm with a designated listener as a callback.
  Alarm(java.lang.String name, AlarmListener listener, java.lang.ClassLoader loader)
          Create a new wakeup alarm with a designated listener as a callback.
  Alarm(java.lang.String name, AlarmListener listener, long delta)
          Creates a named alarm and schedules its wakeup.
  Alarm(java.lang.String name, AlarmListener listener, long delta, java.lang.ClassLoader loader)
          Create a new wakeup alarm with a designated listener as a callback.
 
Method Summary
 void close()
          Closes the alarm instance
 void dequeue()
          Remove the alarm from the wakeup queue.
 java.lang.ClassLoader getContextLoader()
          Sets the alarm's context loader
static long getCurrentTime()
          Returns the approximate current time in milliseconds.
static long getExactTime()
          Returns the exact current time in milliseconds.
static long getExactTimeNanoseconds()
          Returns the exact current time in nanoseconds.
 AlarmListener getListener()
          Return the alarm's listener.
 java.lang.String getName()
          Returns the alarm name.
 long getWakeTime()
          Returns the wake time of this alarm.
 boolean isQueued()
          Returns true if the alarm is currently queued.
static boolean isTest()
          Returns true for testing.
 void queue(long delta)
          Queue the alarm for wakeup.
 void run()
          Runs the alarm.
 void setContextLoader(java.lang.ClassLoader loader)
          Sets the alarm's context loader
 void setListener(AlarmListener listener)
          Sets the alarm's listener.
protected  void setName(java.lang.String name)
          Sets the alarm name.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Alarm

protected Alarm()
Create a new wakeup alarm with a designated listener as a callback. The alarm is not scheduled.


Alarm

public Alarm(AlarmListener listener)
Create a new wakeup alarm with a designated listener as a callback. The alarm is not scheduled.


Alarm

public Alarm(java.lang.String name,
             AlarmListener listener)
Create a new wakeup alarm with a designated listener as a callback. The alarm is not scheduled.


Alarm

public Alarm(java.lang.String name,
             AlarmListener listener,
             java.lang.ClassLoader loader)
Create a new wakeup alarm with a designated listener as a callback. The alarm is not scheduled.


Alarm

public Alarm(java.lang.String name,
             AlarmListener listener,
             long delta,
             java.lang.ClassLoader loader)
Create a new wakeup alarm with a designated listener as a callback. The alarm is not scheduled.


Alarm

public Alarm(java.lang.String name,
             AlarmListener listener,
             long delta)
Creates a named alarm and schedules its wakeup.

Parameters:
name - the object prepared to receive the callback
listener - the object prepared to receive the callback
delta - the time in milliseconds to wake up

Alarm

public Alarm(AlarmListener listener,
             long delta)
Creates a new alarm and schedules its wakeup.

Parameters:
listener - the object prepared to receive the callback
delta - the time in milliseconds to wake up
Method Detail

getName

public java.lang.String getName()
Returns the alarm name.


setName

protected void setName(java.lang.String name)
Sets the alarm name.


getCurrentTime

public static long getCurrentTime()
Returns the approximate current time in milliseconds. Convenient for minimizing system calls.


getExactTime

public static long getExactTime()
Returns the exact current time in milliseconds.


getExactTimeNanoseconds

public static long getExactTimeNanoseconds()
Returns the exact current time in nanoseconds.


isTest

public static boolean isTest()
Returns true for testing.


getWakeTime

public long getWakeTime()
Returns the wake time of this alarm.


getListener

public AlarmListener getListener()
Return the alarm's listener.


setListener

public void setListener(AlarmListener listener)
Sets the alarm's listener.


setContextLoader

public void setContextLoader(java.lang.ClassLoader loader)
Sets the alarm's context loader


getContextLoader

public java.lang.ClassLoader getContextLoader()
Sets the alarm's context loader


isQueued

public boolean isQueued()
Returns true if the alarm is currently queued.


queue

public void queue(long delta)
Queue the alarm for wakeup.

Parameters:
delta - time in milliseconds to wake

dequeue

public void dequeue()
Remove the alarm from the wakeup queue.


run

public void run()
Runs the alarm. This is only called from the worker thread.

Specified by:
run in interface ThreadTask
Specified by:
run in interface java.lang.Runnable

close

public void close()
Closes the alarm instance


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object