public class

Timer

extends Object
java.lang.Object
   ↳ sun.misc.Timer

Class Overview

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.

Summary

Fields
public Timeable owner This is the owner of the timer.
Public Constructors
Timer(Timeable owner, long interval)
Creates a timer object that is owned by 'owner' and with the interval 'interval' milliseconds.
Public Methods
void cont()
Continue the timer.
synchronized long getInterval()
Returns the timer's interval.
synchronized long getRemainingTime()
Returns the remaining time before the timer's next tick.
synchronized long getStopTime()
Returns the time at which the timer was last stopped.
synchronized boolean isStopped()
Returns true if this timer is stopped.
void reset()
Resets the timer's remaining time to the timer's interval.
synchronized void setInterval(long interval)
Changes the timer's interval.
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.
void setRemainingTime(long time)
Sets the remaining time before the timer's next tick.
void stop()
Stops the timer.
Protected Methods
Thread getTimerThread()
[Expand]
Inherited Methods
From class java.lang.Object

Fields

public Timeable owner

This is the owner of the timer. Its tick method is called when the timer ticks.

Public Constructors

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

Public Methods

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.

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.

Parameters
interval new interval of the timer in milliseconds

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.

Parameters
time new remaining time in milliseconds.

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.

Protected Methods

protected Thread getTimerThread ()