Package gda.util
Class Alarm
java.lang.Object
gda.util.Alarm
- All Implemented Interfaces:
Comparable<Object>
Alarm timer service.
Purpose and Responsibilities
Alarm can be used to schedule a callback to anAlarmListener 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
ConstructorsConstructorDescriptionAlarm(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 TypeMethodDescriptionvoidcancel()Cancel a scheduled alarm call.intCompare the expiry time to that of another Alarm.static intReturn the number of currently pending Alarms.Get the optional call-back argument.static StringReturn the string representing the time the execution of the last invoked Alarm started.longReturn the interval for the Alarm.static StringReturn the string representing the time the execution of the last invoked Alarm ended.voidReschedule an alarm call for the same time period as the previous schedule.voidreschedule(long aTimeMillis) Reschedule an alarm call for a given time period.
-
Constructor Details
-
Alarm
Create an alarm call at a specified time from now.- Parameters:
aTimeMillis- time in milliseconds from now of the alarm callaListener- alarm call listener
-
Alarm
Create an alarm call at a specified time from now.- Parameters:
aTimeMillis- time in milliseconds from now of the alarm callaListener- alarm call listeneranArgument- argument that gets passed back to the listener during the alarm call (might benull)
-
-
Method Details
-
getAlarmSize
public static int getAlarmSize()Return the number of currently pending Alarms.- Returns:
- number of alarms
-
getEndLastExecute
Return the string representing the time the execution of the last invoked Alarm started.- Returns:
- string time representation
-
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
Compare the expiry time to that of another Alarm.- Specified by:
compareToin interfaceComparable<Object>- Parameters:
anObject- an alarm instance- Returns:
- the result of the comparison of time
-
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
-