org.iids.aos.util
Class AlarmQueue

java.lang.Object
  extended by org.iids.aos.util.AlarmQueue

public class AlarmQueue
extends java.lang.Object

Class AlarmQueue is a thread-safe alarm queue. This queue can be used to wake up a thread at certain times. Alarms can be set when to wake up. This class is useful in a scenario when periodically certain tasks have to be done, but the time intervals tasks may be variable.

Author:
Michel Oey

Nested Class Summary
static class AlarmQueue.Alarm
          This class represents an alarm.
 
Constructor Summary
AlarmQueue()
           
 
Method Summary
 void addAlarm(AlarmQueue.Alarm newAlarm)
           
 void addAlarm(long time, java.lang.Object id)
           
 void removeAlarm(java.lang.Object id)
          Remove an Alarm.
 AlarmQueue.Alarm waitForAlarm()
          Find the next alarm that has gone off and return it This alarm will have been removed from the AlarmQueue.
 void waitForAlarms(java.util.List<AlarmQueue.Alarm> alarms)
          Wait until the next alarm goes off.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AlarmQueue

public AlarmQueue()
Method Detail

addAlarm

public void addAlarm(AlarmQueue.Alarm newAlarm)

addAlarm

public void addAlarm(long time,
                     java.lang.Object id)

removeAlarm

public void removeAlarm(java.lang.Object id)
Remove an Alarm. It removes the first alarm that matches the object id. Only the first match is removed. For comparison the equals() method is called on the id of each alarm, if not null. If id is null, the first alarm with id null is removed.


waitForAlarm

public AlarmQueue.Alarm waitForAlarm()
                              throws java.lang.InterruptedException
Find the next alarm that has gone off and return it This alarm will have been removed from the AlarmQueue. If no alarms have been set, this function will wait until one has been set and has gone off. Note, if multiple alarms have gone off, only the first one will be returned. The others may be retrieved by multiple calls to this function. Note, if more than one thread is waiting for an alarm, only one thread will get the alarm. It is undetermined which thread will get the alarm.

Returns:
alarm that has gone off
Throws:
java.lang.InterruptedException

waitForAlarms

public void waitForAlarms(java.util.List<AlarmQueue.Alarm> alarms)
                   throws java.lang.InterruptedException
Wait until the next alarm goes off. This function will return all alarms that have gone off in the alarms parameter. These alarms will have been removed from the AlarmQueue. If no alarms have been set, this function will wait until one has been set and has gone off.

Parameters:
alarms - List will contain all alarms that have gone off
Throws:
java.lang.InterruptedException


Copyright © 2003, 2004 IIDS Group. All Rights Reserved.