alianor

Untitled

Dec 14th, 2015
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. package ardi.event;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Queue;
  8. import java.util.concurrent.Executors;
  9. import java.util.concurrent.ScheduledExecutorService;
  10. import java.util.concurrent.TimeUnit;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14. /**
  15. * A class which schedules the execution of {@link Task}s.
  16. *
  17. * @author Graham
  18. */
  19. public final class TaskScheduler implements Runnable {
  20.  
  21. /**
  22. * A logger used to report error messages.
  23. */
  24. private static final Logger logger = Logger.getLogger(TaskScheduler.class
  25. .getName());
  26.  
  27. /**
  28. * The time period, in milliseconds, of a single cycle.
  29. */
  30. private static final int TIME_PERIOD = 600;
  31.  
  32. /**
  33. * The {@link ScheduledExecutorService} which schedules calls to the
  34. * {@link #run()} method.
  35. */
  36. private final ScheduledExecutorService service = Executors
  37. .newSingleThreadScheduledExecutor();
  38.  
  39. /**
  40. * A list of active tasks.
  41. */
  42. private final List<Task> tasks = new ArrayList<Task>();
  43.  
  44. /**
  45. * A queue of tasks that still need to be added.
  46. */
  47. private final Queue<Task> newTasks = new ArrayDeque<Task>();
  48.  
  49. /**
  50. * Creates and starts the task scheduler.
  51. */
  52. public TaskScheduler() {
  53. service.scheduleAtFixedRate(this, 0, TIME_PERIOD, TimeUnit.MILLISECONDS);
  54. }
  55.  
  56. /**
  57. * Stops the task scheduler.
  58. */
  59. /*public void terminate() {
  60. service.shutdown();
  61. }*/
  62.  
  63. /**
  64. * Schedules the specified task. If this scheduler has been stopped with the
  65. * {@link #terminate()} method the task will not be executed or
  66. * garbage-collected.
  67. *
  68. * @param task
  69. * The task to schedule.
  70. */
  71. public void schedule(final Task task) {
  72. if (task.isImmediate()) {
  73. service.execute(new Runnable() {
  74. @Override
  75. public void run() {
  76. task.execute();
  77. }
  78. });
  79. }
  80.  
  81. synchronized (newTasks) {
  82. newTasks.add(task);
  83. }
  84. }
  85.  
  86. /**
  87. * This method is automatically called every cycle by the
  88. * {@link ScheduledExecutorService} and executes, adds and removes
  89. * {@link Task}s. It should not be called directly as this will lead to
  90. * concurrency issues and inaccurate time-keeping.
  91. */
  92. @Override
  93. public void run() {
  94. synchronized (newTasks) {
  95. Task task;
  96. while ((task = newTasks.poll()) != null)
  97. tasks.add(task);
  98. }
  99.  
  100. for (Iterator<Task> it = tasks.iterator(); it.hasNext();) {
  101. Task task = it.next();
  102. try {
  103. if (!task.tick())
  104. it.remove();
  105. } catch (Throwable t) {
  106. //logger.log(Level.SEVERE, "Exception during task execution.", t);
  107. }
  108. }
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment