Package gda.util

Class Alarm

java.lang.Object
gda.util.Alarm
All Implemented Interfaces:
Comparable<Object>

public class Alarm extends Object implements Comparable<Object>
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:
  • reschedule() the alarm call for the same period of time (does not matter if the original period had expired or not)
  • reschedule(long) the alarm call for another period of time (does not matter if the original period had expired or not)
  • cancel() the alarm call

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. Author Just van den Broecke

  • Constructor Summary

    Constructors
    Constructor
    Description
    Alarm(long aTimeMillis, AlarmListener aListener)
    Create an alarm call at a specified time from now.
    Alarm(long aTimeMillis, AlarmListener aListener, Object anArgument)
    Create an alarm call at a specified time from now.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Cancel a scheduled alarm call.
    int
    compareTo(Object anObject)
    Compare the expiry time to that of another Alarm.
    static int
    Return the number of currently pending Alarms.
    Get the optional call-back argument.
    static String
    Return the string representing the time the execution of the last invoked Alarm started.
    long
    Return the interval for the Alarm.
    static String
    Return the string representing the time the execution of the last invoked Alarm ended.
    void
    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, toString, wait, wait, wait
  • Constructor Details

    • 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, 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 Details

    • getAlarmSize

      public static int getAlarmSize()
      Return the number of currently pending Alarms.
      Returns:
      number of alarms
    • getEndLastExecute

      public static String getEndLastExecute()
      Return the string representing the time the execution of the last invoked Alarm started.
      Returns:
      string time representation
    • getStartLastExecute

      public static String getStartLastExecute()
      Return the string representing the time the execution of the last invoked Alarm ended.
      Returns:
      string time representation
    • getIntervalMillis

      public long getIntervalMillis()
      Return the interval for the Alarm.
      Returns:
      millisecond interval
    • cancel

      public void cancel()
      Cancel a scheduled alarm call.
    • compareTo

      public int compareTo(Object anObject)
      Compare the expiry time to that of another Alarm.
      Specified by:
      compareTo in interface Comparable<Object>
      Parameters:
      anObject - an alarm instance
      Returns:
      the result of the comparison of time
    • getArgument

      public Object getArgument()
      Get the optional call-back argument.
      Returns:
      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