Save This Page
Home » openjdk-7 » sun » misc » [javadoc | source]
sun.misc
public class: Timer [javadoc | source]
java.lang.Object
   sun.misc.Timer
A Timer object is used by algorithms that require timed events. For example, in an animation loop, a timer would help in determining when to change frames. A timer has an interval which determines when it "ticks"; that is, a timer delays for the specified interval and then it calls the owner's tick() method. Here's an example of creating a timer with a 5 sec interval:
class Main implements Timeable {
public void tick(Timer timer) {
System.out.println("tick");
}
public static void main(String args[]) {
(new Timer(this, 5000)).cont();
}
}
A timer can be stopped, continued, or reset at any time. A timer's state is not stopped while it's calling the owner's tick() method. A timer can be regular or irregular. If in regular mode, a timer ticks at the specified interval, regardless of how long the owner's tick() method takes. While the timer is running, no ticks are ever discarded. That means that if the owner's tick() method takes longer than the interval, the ticks that would have occurred are delivered immediately. In irregular mode, a timer starts delaying for exactly the specified interval only after the tick() method returns. Synchronization issues: do not hold the timer's monitor while calling any of the Timer operations below otherwise the Timer class will deadlock.
Field Summary
public  Timeable owner    This is the owner of the timer. Its tick method is called when the timer ticks. 
 long interval     
 long sleepUntil     
 long remainingTime     
 boolean regular     
 boolean stopped     
 Timer next     
static  TimerThread timerThread     
Constructor:
 public Timer(Timeable owner,
    long interval) 
    Creates a timer object that is owned by 'owner' and with the interval 'interval' milliseconds. The new timer object is stopped and is regular. getRemainingTime() return 'interval' at this point. getStopTime() returns the time this object was created.
    Parameters:
    owner - owner of the timer object
    interval - interval of the timer in milliseconds
Method from sun.misc.Timer Summary:
cont,   getInterval,   getRemainingTime,   getStopTime,   getTimerThread,   isStopped,   reset,   setInterval,   setRegular,   setRemainingTime,   stop
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from sun.misc.Timer Detail:
 public  void cont() 
    Continue the timer. The next tick will come at getRemainingTime() milliseconds later. If the timer is not stopped, this call will be a no-op. This method is MT-safe; i.e. it is synchronized but for implementation reasons, the synchronized modifier cannot be included in the method declaration.
 public synchronized long getInterval() 
    Returns the timer's interval.
 public synchronized long getRemainingTime() 
    Returns the remaining time before the timer's next tick. The return value is valid only if timer is stopped.
 public synchronized long getStopTime() 
    Returns the time at which the timer was last stopped. The return value is valid only if the timer is stopped.
 protected Thread getTimerThread() 
 public synchronized boolean isStopped() 
    Returns true if this timer is stopped.
 public  void reset() 
    Resets the timer's remaining time to the timer's interval. If the timer's running state is not altered.
 public synchronized  void setInterval(long interval) 
    Changes the timer's interval. The new interval setting does not take effect until after the next tick. This method does not alter the remaining time or the running state of the timer.
 public synchronized  void setRegular(boolean regular) 
    In regular mode, a timer ticks at the specified interval, regardless of how long the owner's tick() method takes. While the timer is running, no ticks are ever discarded. That means that if the owner's tick() method takes longer than the interval, the ticks that would have occurred are delivered immediately. In irregular mode, a timer starts delaying for exactly the specified interval only after the tick() method returns.
 public  void setRemainingTime(long time) 
    Sets the remaining time before the timer's next tick. This method does not alter the timer's running state. This method is MT-safe; i.e. it is synchronized but for implementation reasons, the synchronized modifier cannot be included in the method declaration.
 public  void stop() 
    Stops the timer. The amount of time the timer has already delayed is saved so if the timer is continued, it will only delay for the amount of time remaining. Note that even after stopping a timer, one more tick may still occur. This method is MT-safe; i.e. it is synchronized but for implementation reasons, the synchronized modifier cannot be included in the method declaration.