Class Timeout
A timeout allows the user to perform a certain task after some amount of time has passed. A timeout may be used for example to take action if a network stall occurs.
When a timeout object is created, it is given a task to run and a length of time after which to run the task. However, the timeout is created in an inactive state. To start the timeout countdown, the start() method must be called. Once the timeout is started, the timeout may be cancelled using the cancel() method, or reset via reset(). The reset() method effectively resets the timeout as if cancel() and then start() had been called, but does so in a way that does not create any new objects or start any new threads, and is thus safe to call frequently if needed, rather than calling cancel() followed by start().
- Since:
- 3.1.5
- Author:
- Peter Hollemans
-
Constructor Summary
-
Method Summary
-
Constructor Details
-
Timeout
Creates a new timeout.- Parameters:
length
- the timeout length in milliseconds.task
- the runnable task to perform when the timeout occurs.
-
-
Method Details
-
reset
public void reset()Resets the elapsed time back to zero and continues the timeout. -
start
public void start()Resets the elapsed time and starts the timeout countdown. If the countdown has already started and the cancel method has not been called, this method has no effect. -
cancel
public void cancel()Cancels the timeout. The timeout is stopped and the start method must be called again.
-