1. /*
  2. * @(#)TimerTask.java 1.6 00/02/02
  3. *
  4. * Copyright 1999, 2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.util;
  11. /**
  12. * A task that can be scheduled for one-time or repeated execution by a Timer.
  13. *
  14. * @author Josh Bloch
  15. * @version 1.6, 02/02/00
  16. * @see Timer
  17. * @since 1.3
  18. */
  19. public abstract class TimerTask implements Runnable {
  20. /**
  21. * This object is used to control access to the TimerTask internals.
  22. */
  23. final Object lock = new Object();
  24. /**
  25. * The state of this task, chosen from the constants below.
  26. */
  27. int state = VIRGIN;
  28. /**
  29. * This task has not yet been scheduled.
  30. */
  31. static final int VIRGIN = 0;
  32. /**
  33. * This task is scheduled for execution. If it is a non-repeating task,
  34. * it has not yet been executed.
  35. */
  36. static final int SCHEDULED = 1;
  37. /**
  38. * This non-repeating task has already executed (or is currently
  39. * executing) and has not been cancelled.
  40. */
  41. static final int EXECUTED = 2;
  42. /**
  43. * This task has been cancelled (with a call to TimerTask.cancel).
  44. */
  45. static final int CANCELLED = 3;
  46. /**
  47. * Next execution time for this task in the format returned by
  48. * System.currentTimeMillis, assuming this task is schedule for execution.
  49. * For repeating tasks, this field is updated prior to each task execution.
  50. */
  51. long nextExecutionTime;
  52. /**
  53. * Period in milliseconds for repeating tasks. A positive value indicates
  54. * fixed-rate execution. A negative value indicates fixed-delay execution.
  55. * A value of 0 indicates a non-repeating task.
  56. */
  57. long period = 0;
  58. /**
  59. * Creates a new timer task.
  60. */
  61. protected TimerTask() {
  62. }
  63. /**
  64. * The action to be performed by this timer task.
  65. */
  66. public abstract void run();
  67. /**
  68. * Cancels this timer task. If the task has been scheduled for one-time
  69. * execution and has not yet run, or has not yet been scheduled, it will
  70. * never run. If the task has been scheduled for repeated execution, it
  71. * will never run again. (If the task is running when this call occurs,
  72. * the task will run to completion, but will never run again.)
  73. *
  74. * <p>Note that calling this method from within the <tt>run</tt> method of
  75. * a repeating timer task absolutely guarantees that the timer task will
  76. * not run again.
  77. *
  78. * <p>This method may be called repeatedly; the second and subsequent
  79. * calls have no effect.
  80. *
  81. * @return true if this task is scheduled for one-time execution and has
  82. * not yet run, or this task is scheduled for repeated execution.
  83. * Returns false if the task was scheduled for one-time execution
  84. * and has already run, or if the task was never scheduled, or if
  85. * the task was already cancelled. (Loosely speaking, this method
  86. * returns <tt>true</tt> if it prevents one or more scheduled
  87. * executions from taking place.)
  88. */
  89. public boolean cancel() {
  90. synchronized(lock) {
  91. boolean result = (state == SCHEDULED);
  92. state = CANCELLED;
  93. return result;
  94. }
  95. }
  96. /**
  97. * Returns the <i>scheduled</i> execution time of the most recent
  98. * <i>actual</i> execution of this task. (If this method is invoked
  99. * while task execution is in progress, the return value is the scheduled
  100. * execution time of the ongoing task execution.)
  101. *
  102. * <p>This method is typically invoked from within a task's run method, to
  103. * determine whether the current execution of the task is sufficiently
  104. * timely to warrant performing the scheduled activity:
  105. * <pre>
  106. * public void run() {
  107. * if (System.currentTimeMillis() - scheduledExecutionTime() >=
  108. * MAX_TARDINESS)
  109. * return; // Too late; skip this execution.
  110. * // Perform the task
  111. * }
  112. * </pre>
  113. * This method is typically <i>not</i> used in conjunction with
  114. * <i>fixed-delay execution</i> repeating tasks, as their scheduled
  115. * execution times are allowed to drift over time, and so are not terribly
  116. * significant.
  117. *
  118. * @return the time at which the most recent execution of this task was
  119. * scheduled to occur, in the format returned by Date.getTime().
  120. * The return value is undefined if the task has yet to commence
  121. * its first execution.
  122. * @see Date#getTime()
  123. */
  124. public long scheduledExecutionTime() {
  125. synchronized(lock) {
  126. return (period < 0 ? nextExecutionTime + period
  127. : nextExecutionTime - period);
  128. }
  129. }
  130. }