TimerService.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /** Copyright (c) 2012-2015 committers of YAKINDU and others.
  2. All rights reserved. This program and the accompanying materials
  3. are made available under the terms of the Eclipse Public License v1.0
  4. which accompanies this distribution, and is available at
  5. http://www.eclipse.org/legal/epl-v10.html
  6. Contributors:
  7. committers of YAKINDU - initial API and implementation
  8. � */
  9. package traffic.light;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Timer;
  13. import java.util.TimerTask;
  14. import java.util.concurrent.locks.Lock;
  15. import java.util.concurrent.locks.ReentrantLock;
  16. /**
  17. * Default timer service implementation.
  18. *
  19. */
  20. public class TimerService implements ITimer {
  21. private final Timer timer = new Timer();
  22. private final List<TimeEventTask> timerTaskList = new ArrayList<TimeEventTask>();
  23. private final Lock lock = new ReentrantLock();
  24. /**
  25. * Timer task that reflects a time event. It's internally used by
  26. * {@link TimerService}.
  27. *
  28. */
  29. private class TimeEventTask extends TimerTask {
  30. private ITimerCallback callback;
  31. int eventID;
  32. /**
  33. * Constructor for a time event.
  34. *
  35. * @param callback
  36. * : Set to {@code true} if event should be repeated
  37. * periodically.
  38. *
  39. * @param eventID
  40. * : Index position within the state machine's timeEvent
  41. * array.
  42. */
  43. public TimeEventTask(ITimerCallback callback, int eventID) {
  44. this.callback = callback;
  45. this.eventID = eventID;
  46. }
  47. public void run() {
  48. callback.timeElapsed(eventID);
  49. }
  50. public boolean equals(Object obj) {
  51. if (obj instanceof TimeEventTask) {
  52. return ((TimeEventTask) obj).callback.equals(callback)
  53. && ((TimeEventTask) obj).eventID == eventID;
  54. }
  55. return super.equals(obj);
  56. }
  57. }
  58. public void setTimer(final ITimerCallback callback, final int eventID,
  59. long time, boolean isPeriodic) {
  60. // Create a new TimerTask for given event and store it.
  61. TimeEventTask timerTask = new TimeEventTask(callback, eventID);
  62. lock.lock();
  63. timerTaskList.add(timerTask);
  64. // start scheduling the timer
  65. if (isPeriodic) {
  66. timer.scheduleAtFixedRate(timerTask, time, time);
  67. } else {
  68. timer.schedule(timerTask, time);
  69. }
  70. lock.unlock();
  71. }
  72. public void unsetTimer(ITimerCallback callback, int eventID) {
  73. lock.lock();
  74. int index = timerTaskList.indexOf(new TimeEventTask(callback, eventID));
  75. if (index != -1) {
  76. timerTaskList.get(index).cancel();
  77. timer.purge();
  78. timerTaskList.remove(index);
  79. }
  80. lock.unlock();
  81. }
  82. /**
  83. * Cancel timer service. Use this to end possible timing threads and free
  84. * memory resources.
  85. */
  86. public void cancel() {
  87. lock.lock();
  88. timer.cancel();
  89. timer.purge();
  90. lock.unlock();
  91. }
  92. }