Issue
So I can see a couple different ways to do what I need and I've done a bunch of google/stack overflow searching but can't find really what I'm looking for. I need to run multiple "Countdown timers." I need to have about 6 possibly up to 10 countdown timers running at once all at different times. I have a tab pane on my main program that I am including the FXML and injecting the controllers into. The Timers Tab has a different controller than the main program.
So the 1st question I have is. Since this "tab" is running on a separate controller but is included into the main program, does it run on a separate application thread?
Here is an example of what the included tab FXML looks like...
When I press each start button. I can create a Timeline
and KeyFrame
for each timer. However, I don't really think that is the best way to go about it. Specially once you get up to 10 timelines running at the same time, and definitely if this is not running on a separate application thread from the main program.
I thought about sending each start request to an ExecutorService
and newCacheThreadPool
however I need to be able to update the labels on the GUI with the current time remaining and I understand you are not supposed to do that with background services. Platform.runLater()
maybe?
The other idea was using the Timer
from the java.util.Timer
class. However, I see this as having the same problems as the ExecutorService
when I need to update the GUI Labels. I also understand that the Timer
class only creates one thread and performs it's tasks sequentially. So, that wouldn't work.
Or, should I have a whole other "CountDown" class that I can create new instances of with each one and then start new threads in. However, if I do that, how do I continually update the GUI. I still would have to poll the CountDown class using a timeline
right? So that would defeat the purpose of this whole thing.
Solution
So the 1st question I have is. Since this "tab" is running on a separate controller but is included into the main program, does it run on a separate application thread?
No, there can only be one JavaFX Application instance per JVM, and also one JavaFX Application Thread per JVM.
As for how you could update the timer, it is fine to use Timeline
- one for each timer. Timeline
does not run on separate thread - it is triggered by the underlying "scene graph rendering pulse" which is responsible for updating the JavaFX GUI periodically. Having more Timeline
instances basically just means that there are more listeners that subscribes to the "pulse" event.
public class TimerController {
private final Timeline timer;
private final ObjectProperty<java.time.Duration> timeLeft;
@FXML private Label timeLabel;
public TimerController() {
timer = new Timeline();
timer.getKeyFrames().add(new KeyFrame(Duration.seconds(1), ae -> updateTimer()));
timer.setCycleCount(Timeline.INDEFINITE);
timeLeft = new SimpleObjectProperty<>();
}
public void initialize() {
timeLabel.textProperty().bind(Bindings.createStringBinding(() -> getTimeStringFromDuration(timeLeft.get()), timeLeft));
}
@FXML
private void startTimer(ActionEvent ae) {
timeLeft.set(Duration.ofMinutes(5)); // For example timer of 5 minutes
timer.playFromStart();
}
private void updateTimer() {
timeLeft.set(timeLeft.get().minusSeconds(1));
}
private static String getTimeStringFromDuration(Duration duration) {
// Do the conversion here...
}
}
Of course, you can also use Executor
and other threading methods, provided you update the Label
via Platform.runLater()
. Alternatively, you could use a Task
.
This is a general example when using background thread:
final Duration countdownDuration = Duration.ofSeconds(5);
Thread timer = new Thread(() -> {
LocalTime start = LocalTime.now();
LocalTime current = LocalTime.now();
LocalTime end = start.plus(countDownDuration);
while (end.isAfter(current)) {
current = LocalTime.now();
final Duration elapsed = Duration.between(current, end);
Platform.runLater(() -> timeLeft.set(current)); // As the label is bound to timeLeft, this line must be inside Platform.runLater()
Thread.sleep(1000);
}
});
Answered By - Jai