RuntimeService.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package traffic.light;
  2. import java.util.HashMap;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Timer;
  7. import java.util.TimerTask;
  8. import java.util.concurrent.locks.ReentrantReadWriteLock;
  9. /**
  10. * Runtime service for state machines to execute a run to completion step
  11. * periodically.
  12. *
  13. */
  14. public class RuntimeService {
  15. private static RuntimeService runtimeService;
  16. private Timer timer = null;
  17. private Map<Long, StatemachineTimerTask> timerTasks = new HashMap<Long, StatemachineTimerTask>();
  18. private class StatemachineTimerTask extends TimerTask {
  19. private List<IStatemachine> statemachineList = new LinkedList<IStatemachine>();
  20. private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
  21. private boolean isPaused = false;
  22. @Override
  23. public void run() {
  24. lock.readLock().lock();
  25. if (!isPaused) {
  26. for (IStatemachine statemachine : statemachineList) {
  27. statemachine.runCycle();
  28. }
  29. }
  30. lock.readLock().unlock();
  31. }
  32. /**
  33. * Adds the given state machine to the TimerTask.
  34. *
  35. * @param statemachine
  36. * @return {@code true} if state machine is added properly.
  37. */
  38. public boolean addStatemachine(IStatemachine statemachine) {
  39. lock.writeLock().lock();
  40. boolean ret = statemachineList.add(statemachine);
  41. lock.writeLock().unlock();
  42. return ret;
  43. }
  44. /**
  45. * Removes the given state machine from the TimerTask.
  46. *
  47. * @param statemachine
  48. * @return {@code true} if state machine is removed properly.
  49. */
  50. public boolean removeStatemachine(IStatemachine statemachine) {
  51. lock.writeLock().lock();
  52. boolean ret = statemachineList.remove(statemachine);
  53. lock.writeLock().unlock();
  54. return ret;
  55. }
  56. public void pause() {
  57. isPaused = true;
  58. }
  59. public void resume() {
  60. isPaused = false;
  61. }
  62. }
  63. private RuntimeService() {
  64. // Not intended to be instantiated.
  65. }
  66. /**
  67. * Returns the {@code RuntimeService} instance as singleton.
  68. *
  69. * @return The singleton {@code RuntimeService} instance
  70. */
  71. public static RuntimeService getInstance() {
  72. if (runtimeService == null) {
  73. runtimeService = new RuntimeService();
  74. }
  75. return runtimeService;
  76. }
  77. /**
  78. * Registers an {@link IStatemachine} for scheduled fixed rate execution
  79. *
  80. * @param statemachine
  81. * - The state machine to execute
  82. * @param cyclePeriod
  83. * - the fixed rate cycle period for scheduling
  84. * @return {@code true} if state machine is added properly.
  85. */
  86. public boolean registerStatemachine(IStatemachine statemachine,
  87. long cyclePeriod) {
  88. if (timerTasks.containsKey(cyclePeriod)) {
  89. // TimerTask for cycle time already existing -> add state machine
  90. return timerTasks.get(cyclePeriod).addStatemachine(statemachine);
  91. } else {
  92. // Create new TimerTask for cycle period and add state machine
  93. StatemachineTimerTask timerTask = new StatemachineTimerTask();
  94. timerTasks.put(cyclePeriod, timerTask);
  95. boolean ret = timerTask.addStatemachine(statemachine);
  96. // Create a new Timer instance if runtime service was cancelled
  97. // before
  98. if (timer == null) {
  99. timer = new Timer();
  100. }
  101. timer.scheduleAtFixedRate(timerTask, 0, cyclePeriod);
  102. return ret;
  103. }
  104. }
  105. /**
  106. * Removes the given state machine from runtime service.
  107. *
  108. * @param statemachine
  109. * - the state machine to be removed
  110. * @param cyclePeriod
  111. * - the scheduling cycle period of the state machine
  112. * @return {@code true} if state machine is removed properly.
  113. */
  114. public boolean unregisterStatemachine(IStatemachine statemachine,
  115. long cyclePeriod) {
  116. if (timerTasks.containsKey(cyclePeriod)) {
  117. boolean ret = timerTasks.get(cyclePeriod).removeStatemachine(
  118. statemachine);
  119. return ret;
  120. }
  121. return false;
  122. }
  123. /**
  124. * Cancels the execution of state machines for the given cycle period. This
  125. * stops the execution of state machines which are registered for the given
  126. * cycle period and cancels the executing {@link TimerTask}.
  127. *
  128. * @return {@code true} if poperly cancelled
  129. */
  130. public boolean cancelAll(long cyclePeriod) {
  131. if (timer != null && timerTasks.containsKey(cyclePeriod)) {
  132. TimerTask task = timerTasks.get(cyclePeriod);
  133. task.cancel();
  134. timer.purge();
  135. timerTasks.remove(cyclePeriod);
  136. return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * Pauses the execution of all state machines which are registered for the
  142. * given cyclePeriod.
  143. *
  144. * @param cyclePeriod
  145. * @return {@code true} if properly paused
  146. *
  147. */
  148. public boolean pauseAll(long cyclePeriod) {
  149. if (timerTasks.containsKey(cyclePeriod)) {
  150. timerTasks.get(cyclePeriod).pause();
  151. return true;
  152. }
  153. return false;
  154. }
  155. /**
  156. * Resumes the execution of all state machines which are registered for the
  157. * given cyclePeriod.
  158. *
  159. * @param cyclePeriod
  160. * @return {@code true} if properly resumed
  161. *
  162. */
  163. public boolean resumeAll(long cyclePeriod) {
  164. if (timerTasks.containsKey(cyclePeriod)) {
  165. timerTasks.get(cyclePeriod).resume();
  166. return true;
  167. }
  168. return false;
  169. }
  170. /**
  171. * Cancels the execution of all registered state machines. This cancels the
  172. * executing {@link Timer} freeing all allocated resources and terminates
  173. * all existing execution threads.
  174. */
  175. public void cancelTimer() {
  176. if (timer != null) {
  177. timer.cancel();
  178. timer.purge();
  179. timerTasks.clear();
  180. timer = null;
  181. }
  182. }
  183. }