nl.justobjects.toolkit.sys
Class Alarm

java.lang.Object
  |
  +--nl.justobjects.toolkit.sys.Alarm
All Implemented Interfaces:
java.lang.Comparable

public class Alarm
extends java.lang.Object
implements java.lang.Comparable

Alarm timer service.

Purpose and Responsibilities

Alarm can be used to schedule a callback to an AlarmListener object. This callback is performed once after a specified amount of time (ms) has elapsed. After creation, a client may:

Usage and Examples

A client of Alarm has to instantiate an Alarm object with a specified amount of time and a callback AlarmListener object. Optionally an argument to be passed on the callback can be specified.

 Alarm alarm = new Alarm(5000,
     new AlarmListener() {
         public void alarm(Alarm anAlarm) {
             // do something here
         }
     });

 // reschedule the alarm on the previously specified time.
 alarm.reschedule();

 // reschedule the alarm on another time
 alarm.reschedule(3000);

 // cancel the callback before it is executed
 alarm.cancel();
 

Implementation

One thread loops infinitely executing expired alarms and sleeping until the next alarm expires and must be executed.

Concurrency

This class is thread-safe, because all read/write accesses on the scheduled alarms set are synchronized.

Special effort has been made to enable cancellations or rescheduling of alarm calls by clients within a alarm callback.

Version:
$Id: Alarm.java,v 1.7 2003/01/06 00:23:49 just Exp $
Author:
Just van den Broecke

Inner Class Summary
private static class Alarm.AlarmThread
          Alarm timer thread.
 
Field Summary
private static java.util.SortedSet alarms
          map of scheduled alarms
private static Alarm.AlarmThread alarmThread
          the schedule thread
private  java.lang.Object arg
          optional call-back argument
private  long deltaTimeMillis
          delta between the moment of scheduling and expiration
private static java.lang.String endLastExecute
          contains the system time of the end of the execution of the last alarm
private  long expireTimeMillis
          absolute system time of expiration
private  AlarmListener listener
          alarm call-back listener
private static java.lang.String startLastExecute
          contains the system time of the begining of the execution of the last alarm
 
Constructor Summary
Alarm(long aTimeMillis, AlarmListener aListener)
          Create an alarm call at a specified time from now.
Alarm(long aTimeMillis, AlarmListener aListener, java.lang.Object anArgument)
          Create an alarm call at a specified time from now.
 
Method Summary
(package private) static void ()
           
 void cancel()
          Cancel a scheduled alarm call.
 int compareTo(java.lang.Object anObject)
          Compare the expiry time to that of another Alarm.
private  void execute()
          Execute the alarm callback.
static int getAlarmSize()
          Retutn the number of currently pending Alarms.
 java.lang.Object getArgument()
          Get the optional call-back argument.
static java.lang.String getEndLastExecute()
          Return the string representing the time the execution of the last invoked Alarm started.
 long getIntervalMillis()
          Return the interval for the Alarm.
private  long getRemainingMillis()
          Get the remaining millis before expiration.
static java.lang.String getStartLastExecute()
          Return the string representing the time the execution of the last invoked Alarm ended.
private  boolean isExpired()
          Is this alarm call expired?
 void reschedule()
          Reschedule an alarm call for the same time period as the previous schedule.
 void reschedule(long aTimeMillis)
          Reschedule an alarm call for a given time period.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

alarmThread

private static Alarm.AlarmThread alarmThread
the schedule thread

alarms

private static java.util.SortedSet alarms
map of scheduled alarms

startLastExecute

private static java.lang.String startLastExecute
contains the system time of the begining of the execution of the last alarm

endLastExecute

private static java.lang.String endLastExecute
contains the system time of the end of the execution of the last alarm

arg

private java.lang.Object arg
optional call-back argument

deltaTimeMillis

private long deltaTimeMillis
delta between the moment of scheduling and expiration

expireTimeMillis

private long expireTimeMillis
absolute system time of expiration

listener

private AlarmListener listener
alarm call-back listener
Constructor Detail

Alarm

public Alarm(long aTimeMillis,
             AlarmListener aListener)
Create an alarm call at a specified time from now.
Parameters:
aTimeMillis - time in milliseconds from now of the alarm call
aListener - alarm call listener

Alarm

public Alarm(long aTimeMillis,
             AlarmListener aListener,
             java.lang.Object anArgument)
Create an alarm call at a specified time from now.
Parameters:
aTimeMillis - time in milliseconds from now of the alarm call
aListener - alarm call listener
anArgument - argument that gets passed back to the listener during the alarm call (might be null)
Method Detail

getAlarmSize

public static int getAlarmSize()
Retutn the number of currently pending Alarms.

getEndLastExecute

public static java.lang.String getEndLastExecute()
Return the string representing the time the execution of the last invoked Alarm started.

getStartLastExecute

public static java.lang.String getStartLastExecute()
Return the string representing the time the execution of the last invoked Alarm ended.

getIntervalMillis

public long getIntervalMillis()
Return the interval for the Alarm.

cancel

public void cancel()
Cancel a scheduled alarm call.

compareTo

public int compareTo(java.lang.Object anObject)
Compare the expiry time to that of another Alarm.
Specified by:
compareTo in interface java.lang.Comparable
Parameters:
anObject - an alarm instance

getArgument

public java.lang.Object getArgument()
Get the optional call-back argument.

reschedule

public void reschedule()
Reschedule an alarm call for the same time period as the previous schedule.

reschedule

public void reschedule(long aTimeMillis)
Reschedule an alarm call for a given time period.
Parameters:
aTimeMillis - time in milliseconds from now of the alarm call

execute

private void execute()
Execute the alarm callback.

getRemainingMillis

private long getRemainingMillis()
Get the remaining millis before expiration.

isExpired

private boolean isExpired()
Is this alarm call expired?

static void ()


Copyright © 2000-2001 - Just Objects B.V.