com.caucho.util
Class Alarm

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

public class Alarm
extends java.lang.Object
implements ThreadTask, ClassLoaderListener

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.
protected Alarm(java.lang.String name)
           
  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
protected  void addEnvironmentListener()
          Registers the alarm with the environment listener for auto-close
 void classLoaderDestroy(DynamicClassLoader loader)
          Handles the case where a class loader is dropped.
 void classLoaderInit(DynamicClassLoader loader)
          Handles the case where a class loader has completed initialization
 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 getCurrentTimeActual()
          Gets current time, handling test
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.
static boolean isActive()
           
 boolean isPriority()
          True for a priority alarm (default)
 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 queueAt(long wakeTime)
          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.
 void setPriority(boolean isPriority)
          True for a priority alarm (default)
 void setWakeTime(long wakeTime)
           
 java.lang.String toString()
           
static void yieldIfTest()
          Yield if in test mode to maintain ordering
 
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

protected Alarm(java.lang.String name)

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.


isActive

public static boolean isActive()

getCurrentTime

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


getCurrentTimeActual

public static long getCurrentTimeActual()
Gets current time, handling test


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.


yieldIfTest

public static void yieldIfTest()
Yield if in test mode to maintain ordering


getWakeTime

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


setWakeTime

public void setWakeTime(long wakeTime)

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.


setPriority

public void setPriority(boolean isPriority)
True for a priority alarm (default)


isPriority

public boolean isPriority()
True for a priority alarm (default)


addEnvironmentListener

protected void addEnvironmentListener()
Registers the alarm with the environment listener for auto-close


queue

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

Parameters:
delta - time in milliseconds to wake

queueAt

public void queueAt(long wakeTime)
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

classLoaderInit

public void classLoaderInit(DynamicClassLoader loader)
Handles the case where a class loader has completed initialization

Specified by:
classLoaderInit in interface ClassLoaderListener

classLoaderDestroy

public void classLoaderDestroy(DynamicClassLoader loader)
Handles the case where a class loader is dropped.

Specified by:
classLoaderDestroy in interface ClassLoaderListener

close

public void close()
Closes the alarm instance


toString

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