public class Timer
extends java.lang.Object
TimerListener.onTimeout(Timer)
method when the time expires.
Timer has to be explicitly started, and can be halted before it expires.
A timer may run is 'daemon' mode or 'non-daemon' mode.
In 'daemon' mode, if all program threads terminate, the timer terminates silently,
without firing the corresponding timeout callback.
Conversely, in 'non-daemon' mode, the program terminates only when the timer
expires (or it is explicitly halted).
Modifier and Type | Field and Description |
---|---|
static boolean |
DEFAULT_DAEMON_MODE
Whether the default mode is 'daemon', or not
|
protected boolean |
is_running
Whether this timer is running
|
protected TimerListener |
listener
Timer listener
|
protected long |
time
Timeout value
|
Constructor and Description |
---|
Timer(long time,
TimerListener listener)
Creates a new Timer.
|
Modifier and Type | Method and Description |
---|---|
long |
getExpirationTime()
Gets the remaining time.
|
long |
getTime()
Gets the initial time.
|
void |
halt()
Stops the Timer.
|
boolean |
isRunning()
Whether the timer is running.
|
void |
start()
Starts the timer.
|
void |
start(boolean daemon_mode)
Starts the timer.
|
public static boolean DEFAULT_DAEMON_MODE
protected long time
protected boolean is_running
protected TimerListener listener
public Timer(long time, TimerListener listener)
The Timer is not automatically started. You need to call the start()
method.
time
- the timer expiration time in millisecondslistener
- timer listenerpublic long getTime()
public long getExpirationTime()
public void start()
public void start(boolean daemon_mode)
daemon_mode
- whether running in 'daemon' mode
In 'daemon' mode, when all other threads terminate, the program also ends
regardless the timer was still running, and no timeout callback is fired.
In 'non-daemon' mode, the program ends only when all active timers have expired
or explicitly halted.public boolean isRunning()
public void halt()
TimerListener.onTimeout(Timer)
of the timer listener will not be fired.