TimerService.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package traffic.light;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Timer;
  5. import java.util.TimerTask;
  6. import java.util.concurrent.locks.Lock;
  7. import java.util.concurrent.locks.ReentrantLock;
  8. /**
  9. * Default timer service implementation.
  10. *
  11. */
  12. public class TimerService implements ITimer {
  13. private final Timer timer = new Timer();
  14. private final List<TimeEventTask> timerTaskList = new ArrayList<TimeEventTask>();
  15. private final Lock lock = new ReentrantLock();
  16. /**
  17. * Timer task that reflects a time event. It's internally used by
  18. * {@link TimerService}.
  19. *
  20. */
  21. private class TimeEventTask extends TimerTask {
  22. private ITimerCallback callback;
  23. int eventID;
  24. /**
  25. * Constructor for a time event.
  26. *
  27. * @param callback
  28. * : Object that implements ITimerCallback, is called
  29. * when the timer expires.
  30. *
  31. * @param eventID
  32. * : Index position within the state machine's timeEvent
  33. * array.
  34. */
  35. public TimeEventTask(ITimerCallback callback, int eventID) {
  36. this.callback = callback;
  37. this.eventID = eventID;
  38. }
  39. public void run() {
  40. callback.timeElapsed(eventID);
  41. }
  42. @Override
  43. public boolean equals(Object obj) {
  44. if (obj instanceof TimeEventTask) {
  45. return ((TimeEventTask) obj).callback.equals(callback)
  46. && ((TimeEventTask) obj).eventID == eventID;
  47. }
  48. return super.equals(obj);
  49. }
  50. @Override
  51. public int hashCode() {
  52. int prime = 37;
  53. int result = 1;
  54. int c = (int) this.eventID;
  55. result = prime * result + c;
  56. c = this.callback.hashCode();
  57. result = prime * result + c;
  58. return result;
  59. }
  60. }
  61. public void setTimer(final ITimerCallback callback, final int eventID,
  62. long time, boolean isPeriodic) {
  63. // Create a new TimerTask for given event and store it.
  64. TimeEventTask timerTask = new TimeEventTask(callback, eventID);
  65. lock.lock();
  66. timerTaskList.add(timerTask);
  67. // start scheduling the timer
  68. if (isPeriodic) {
  69. timer.scheduleAtFixedRate(timerTask, time, time);
  70. } else {
  71. timer.schedule(timerTask, time);
  72. }
  73. lock.unlock();
  74. }
  75. public void unsetTimer(ITimerCallback callback, int eventID) {
  76. lock.lock();
  77. int index = timerTaskList.indexOf(new TimeEventTask(callback, eventID));
  78. if (index != -1) {
  79. timerTaskList.get(index).cancel();
  80. timer.purge();
  81. timerTaskList.remove(index);
  82. }
  83. lock.unlock();
  84. }
  85. /**
  86. * Cancel timer service. Use this to end possible timing threads and free
  87. * memory resources.
  88. */
  89. public void cancel() {
  90. lock.lock();
  91. timer.cancel();
  92. timer.purge();
  93. lock.unlock();
  94. }
  95. }