Implements a timer that runs Actions at a predefined point in time or after a specified delay @param Type of the Action's unique identifier @param Type (e.g., subclass) of Action instances used by this timer @author Robert Altnoeder <robert.altnoeder@linbit.com>
| 21 | * @author Robert Altnoeder <robert.altnoeder@linbit.com> |
| 22 | */ |
| 23 | public class GenericTimer<K extends Comparable<K>, V extends Action<K>> |
| 24 | implements Timer<K, V>, SystemService |
| 25 | { |
| 26 | private static final ServiceName SERVICE_NAME; |
| 27 | private static final String SERVICE_INFO = "Timed actions scheduler"; |
| 28 | |
| 29 | // Maps interrupt time value to action id & action object |
| 30 | private final TreeMap<Long, TreeMap<K, V>> timerMap; |
| 31 | |
| 32 | // Maps action id to interrupt time value |
| 33 | private final TreeMap<K, Long> actionMap; |
| 34 | |
| 35 | private boolean stopFlag = false; |
| 36 | |
| 37 | private long schedWakeupTime = 0; |
| 38 | |
| 39 | private @Nullable ActionScheduler<K, V> sched; |
| 40 | |
| 41 | private ServiceName serviceInstanceName; |
| 42 | |
| 43 | static |
| 44 | { |
| 45 | try |
| 46 | { |
| 47 | SERVICE_NAME = new ServiceName("TimerEventService"); |
| 48 | } |
| 49 | catch (InvalidNameException nameExc) |
| 50 | { |
| 51 | throw new ImplementationError( |
| 52 | String.format( |
| 53 | "%s class contains an invalid name constant", |
| 54 | GenericTimer.class.getName() |
| 55 | ), |
| 56 | nameExc |
| 57 | ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Constructs a new timer instance |
| 63 | */ |
| 64 | public GenericTimer() |
| 65 | { |
| 66 | timerMap = new TreeMap<>(); |
| 67 | actionMap = new TreeMap<>(); |
| 68 | sched = null; |
| 69 | serviceInstanceName = SERVICE_NAME; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Adds an action to perform after a delay |
| 74 | * |
| 75 | * Note that actions are performed asynchronously and that a newly added action may |
| 76 | * be performed before this method returns. |
| 77 | * |
| 78 | * Actions with a delay of zero are performed immediately in the context of the thread that called |
| 79 | * the addDelayedAction() method. |
| 80 | * |