TimerTask.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* TimerTask.java -- Task that can be run at a later time if given to a Timer.
  2. Copyright (C) 2000 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. /**
  33. * Task that can be run at a later time if given to a Timer.
  34. * The TimerTask must implement a run method that will be called by the
  35. * Timer when the task is scheduled for execution. The task can check when
  36. * it should have been scheduled and cancel itself when no longer needed.
  37. * <p>
  38. * Example:
  39. * <pre>
  40. * Timer timer = new Timer();
  41. * TimerTask task = new TimerTask() {
  42. * public void run() {
  43. * if (this.scheduledExecutionTime() &lt; System.currentTimeMillis() + 500)
  44. * // Do something
  45. * else
  46. * // Complain: We are more then half a second late!
  47. * if (someStopCondition)
  48. * this.cancel(); // This was our last execution
  49. * };
  50. * timer.scheduleAtFixedRate(task, 1000, 1000); // schedule every second
  51. * </pre>
  52. * <p>
  53. * Note that a TimerTask object is a one shot object and can only given once
  54. * to a Timer. (The Timer will use the TimerTask object for bookkeeping,
  55. * in this implementation).
  56. * <p>
  57. * This class also implements <code>Runnable</code> to make it possible to
  58. * give a TimerTask directly as a target to a <code>Thread</code>.
  59. *
  60. * @see Timer
  61. * @since 1.3
  62. * @author Mark Wielaard (mark@klomp.org)
  63. */
  64. public abstract class TimerTask implements Runnable
  65. {
  66. /**
  67. * If positive the next time this task should be run.
  68. * If negative this TimerTask is canceled or executed for the last time.
  69. */
  70. long scheduled;
  71. /**
  72. * If positive the last time this task was run.
  73. * If negative this TimerTask has not yet been scheduled.
  74. */
  75. long lastExecutionTime;
  76. /**
  77. * If positive the number of milliseconds between runs of this task.
  78. * If -1 this task doesn't have to be run more then once.
  79. */
  80. long period;
  81. /**
  82. * If true the next time this task should be run is relative to
  83. * the last scheduled time, otherwise it can drift in time.
  84. */
  85. boolean fixed;
  86. /**
  87. * Creates a TimerTask and marks it as not yet scheduled.
  88. */
  89. protected TimerTask()
  90. {
  91. this.scheduled = 0;
  92. this.lastExecutionTime = -1;
  93. }
  94. /**
  95. * Marks the task as canceled and prevents any further execution.
  96. * Returns true if the task was scheduled for any execution in the future
  97. * and this cancel operation prevents that execution from happening.
  98. * <p>
  99. * A task that has been canceled can never be scheduled again.
  100. * <p>
  101. * In this implementation the TimerTask it is possible that the Timer does
  102. * keep a reference to the TimerTask until the first time the TimerTask
  103. * is actually scheduled. But the reference will disappear immediatly when
  104. * cancel is called from within the TimerTask run method.
  105. */
  106. public boolean cancel()
  107. {
  108. boolean prevented_execution = (this.scheduled >= 0);
  109. this.scheduled = -1;
  110. return prevented_execution;
  111. }
  112. /**
  113. * Method that is called when this task is scheduled for execution.
  114. */
  115. public abstract void run();
  116. /**
  117. * Returns the last time this task was scheduled or (when called by the
  118. * task from the run method) the time the current execution of the task
  119. * was scheduled. When the task has not yet run the return value is
  120. * undefined.
  121. * <p>
  122. * Can be used (when the task is scheduled at fixed rate) to see the
  123. * difference between the requested schedule time and the actual time
  124. * that can be found with <code>System.currentTimeMillis()</code>.
  125. */
  126. public long scheduledExecutionTime()
  127. {
  128. return lastExecutionTime;
  129. }
  130. }