JavaFX Running a large amount of countdown timers at once?

前端 未结 4 1115
面向向阳花
面向向阳花 2021-01-28 10:04

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 mult

相关标签:
4条回答
  • 2021-01-28 10:31

    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);
        }
    });
    
    0 讨论(0)
  • 2021-01-28 10:34

    Here is another way, standard java. Depending on how long the countdown runs one may want to stop those executors when the GUI is closed. I am using the ScheduledExecutorService also for multiple countdowns.

    import java.time.LocalTime;
    import java.time.format.DateTimeFormatter;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.atomic.AtomicInteger;
    
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    public class CountDownExecutor extends Application{
    
        private final int i= 15;
        private final DateTimeFormatter HH_MM_SS = DateTimeFormatter.ofPattern("HH:mm:ss");
        private final Label l1=new Label("00:00:00");
        private final Insets insets = new Insets(3,5,3,5);
        private final Button button = new Button("Start");
    
        private ScheduledExecutorService executor=null;
        private AtomicInteger atomicInteger = new AtomicInteger();
    
        public static void main(String[] args) {
            Application.launch(CountDownExecutor.class, args);
        }
    
        @Override
        public void start(Stage stage) {
            HBox hb = new HBox();
            button.setOnMouseClicked(a-> countDown());
            button.setPadding(insets);
            l1.setPadding(insets);
            hb.getChildren().addAll(button,l1);
            Scene scene = new Scene(hb);
            stage.setOnCloseRequest((ev)-> {if(executor!=null) executor.shutdownNow();});
            stage.setScene(scene);
            stage.show();
        }
    
        public void countDown() {
            Platform.runLater( () -> button.setDisable(true));
            atomicInteger.set(i);
            setCountDown(LocalTime.ofSecondOfDay(atomicInteger.get()));
            executor = Executors.newScheduledThreadPool(1);
    
            Runnable r = ()->{
                int j = atomicInteger.decrementAndGet();
                if(j<1 ){
                    executor.shutdown();
                    Platform.runLater( () ->{ 
                        button.setDisable(false);
                    });
                    setCountDown(LocalTime.ofSecondOfDay(0));
                }else {
                    setCountDown(LocalTime.ofSecondOfDay(j));
                }
            };
            executor.scheduleAtFixedRate(r, 1, 1, TimeUnit.SECONDS);
        }
    
        public void setCountDown(LocalTime lt)  { Platform.runLater(() -> l1.setText(lt.format(HH_MM_SS))); }
    } 
    
    0 讨论(0)
  • 2021-01-28 10:39

    What you are looking for is RxJava and its bridge to JavaFx which is RxJavaFx. Import dependency:

    <dependency>
        <groupId>io.reactivex.rxjava2</groupId>
        <artifactId>rxjavafx</artifactId>
        <version>2.2.2</version>
    </dependency>
    

    and run

    import java.util.concurrent.TimeUnit;
    
    import io.reactivex.Observable;
    import io.reactivex.rxjavafx.observables.JavaFxObservable;
    import io.reactivex.rxjavafx.schedulers.JavaFxScheduler;
    import io.reactivex.schedulers.Schedulers;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.ToggleButton;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class TimersApp extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage stage) throws Exception {
    
            VBox vBox = new VBox();
            for (int i = 0; i < 4; i++) {
                ToggleButton button = new ToggleButton("Start");
                Label label = new Label("0");
                HBox hBox = new HBox(button, label, new Label("seconds"));
                vBox.getChildren().add(hBox);
    
                JavaFxObservable.valuesOf(button.selectedProperty())
                .switchMap(selected -> {
                    if (selected) {
                        button.setText("Stop");
                        return Observable.interval(1, TimeUnit.SECONDS, Schedulers.computation()).map(next -> ++next);
                    } else {
                        button.setText("Start");
                        return Observable.empty();
                    }
                })
                .map(String::valueOf)
                .observeOn(JavaFxScheduler.platform())
                .subscribe(label::setText);
            }
    
            stage.setScene(new Scene(vBox));
            stage.show();
        }
    }
    

    Let me know if you are intrested in this solution. I will provide you some materials to learn.

    0 讨论(0)
  • 2021-01-28 10:53

    To add to the good answer posted by Jai, you could test different implementations for performance and find out if they use separate threads by a simple printout:

    import java.io.IOException;
    import javafx.animation.Animation;
    import javafx.animation.KeyFrame;
    import javafx.animation.PauseTransition;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.concurrent.Task;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    public class TimersTest extends Application {
    
        @Override public void start(Stage stage) throws IOException {
    
            System.out.println("Fx thread id "+ Thread.currentThread().getId());
    
            VBox root = new VBox(new TimeLineCounter(), new PauseTransitionCounter(), new TaskCounter());
            stage.setScene(new Scene(root));
            stage.show();
        }
    
        public static void main(String[] args) { launch(args); }
    }
    
    abstract class Counter extends Label {
    
        protected int count = 0;
        public Counter() {
            setAlignment(Pos.CENTER); setPrefSize(25, 25);
            count();
        }
    
        abstract void count();
    }
    
    class TimeLineCounter extends Counter {
    
        @Override
        void count() {
    
            Timeline timeline = new Timeline();
            timeline.setCycleCount(Animation.INDEFINITE);
            KeyFrame keyFrame = new KeyFrame(
                    Duration.seconds(1),
                    event -> {  setText(String.valueOf(count++) );  }
            );
            timeline.getKeyFrames().add(keyFrame);
            System.out.println("TimeLine thread id "+ Thread.currentThread().getId());
            timeline.play();
        }
    }
    
    class PauseTransitionCounter extends Counter {
    
        @Override
        void count() {
    
            PauseTransition pauseTransition = new PauseTransition(Duration.seconds(1));
            pauseTransition.setOnFinished(event ->{
                setText(String.valueOf(count++) );
                pauseTransition.play();
            });
            System.out.println("PauseTransition thread id "+ Thread.currentThread().getId());
            pauseTransition.play();
        }
    }
    
    class TaskCounter extends Counter {
    
        @Override
        void count() { count(this); }
    
        void count(final Label label) {
    
             Task<Void> counterTask = new Task<>() {
                    @Override
                    protected Void call() throws Exception {
                        try {
                            System.out.println("Task counter thread id "+ Thread.currentThread().getId());
                            while(true){
                                Platform.runLater(() -> label.setText(String.valueOf(count++)));
                                Thread.sleep(1000);
                            }
                        } catch (InterruptedException e) {e.printStackTrace();     }
                        return null;
                    }
                };
    
                Thread th = new Thread(counterTask);   th.setDaemon(true);    th.start();
        }
    }
    

    The printout shows, as expected, that Timeline and PauseTransition are on the FX thread, while Task is not:

    Fx thread id 15
    TimeLine thread id 15
    PauseTransition thread id 15
    Task counter thread id 19

    0 讨论(0)
提交回复
热议问题