public interface Timer
Every timer also has an associated thread (such as JavaFX application
thread or a single-thread executor's thread). Timer may only be accessed
from its associated thread. Timer's action is executed on its associated
thread, too. This design allows to implement guarantees provided by
stop().
| Modifier and Type | Method and Description |
|---|---|
void |
restart()
Schedules the associated action to be executed after the associated
delay.
|
void |
stop()
If the associated action has been scheduled for execution but not yet
executed, this method prevents it from being executed at all.
|
void restart()
void stop()
Animation.stop():
Timeline timeline = new Timeline(new KeyFrame(
Duration.millis(1000),
ae -> System.out.println("FIRED ANYWAY")));
timeline.play();
// later on the JavaFX application thread,
// but still before the action has been executed
timeline.stop();
// later, "FIRED ANYWAY" may still be printed
In contrast, using the FxTimer, the action is guaranteed not to
be executed after stop():
Timer timer = FxTimer.runLater(
Duration.ofMillis(1000),
() -> System.out.println("FIRED"));
// later on the JavaFX application thread,
// but still before the action has been executed
timer.stop();
// "FIRED" is guaranteed *not* to be printed