繁体   English   中英

如何正确退出javaFX Platform.runLater

[英]How to properly exit javaFX Platform.runLater

当我关闭应用程序时,下面的代码无法正确退出。 我相信问题是我应该在哪里准确地调用system.exit和platform.exit...。

hour_Label.textProperty().bind(hour);
minute_Label.textProperty().bind(minute);
second_Label.textProperty().bind(second);

new Thread(() -> 
{    
    for (;;) 
    {                 
        try 
        { 
            final SimpleDateFormat simpledate_hour = new SimpleDateFormat("h");
            final SimpleDateFormat simpledate_minute = new SimpleDateFormat("mm");
            final SimpleDateFormat simpledate_second = new SimpleDateFormat("s");
            Platform.runLater(new Runnable() {
                @Override public void run() 
                {
                    hour.set(simpledate_hour.format(new Date()));
                    minute.set(simpledate_minute.format(new Date()));
                    second.set(simpledate_second.format(new Date())); 
                }
            });
            Thread.sleep(200);                
        }
        catch (Exception e){logger.warn("Unexpected error", e); Thread.currentThread().interrupt(); Platform.exit(); System.exit(0);}
    }
}).start();

使您的线程成为守护线程

当仅运行的所有线程都是守护程序线程时,Java虚拟机将退出。

您还需要让线程知道它应该退出。

import javafx.application.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.Date;
import java.util.concurrent.atomic.AtomicBoolean;

public class Sleeper extends Application{
    @Override
    public void start(Stage stage) throws Exception {
        Label time = new Label();

        AtomicBoolean shuttingDown = new AtomicBoolean(false);

        Thread thread = new Thread(() -> {
            while (!shuttingDown.get() && !Thread.interrupted()) {
                Platform.runLater(() -> time.setText(new Date().toString()));
                try {
                    Thread.sleep(1_000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
        thread.setDaemon(true);
        thread.start();

        Button exit = new Button("Exit");
        exit.setOnAction(event -> {
            shuttingDown.set(true);
            thread.interrupt();
            Platform.exit();
        });

        stage.setScene(new Scene(new StackPane(time), 240, 40));
        stage.show();
    }
}

您无需在线程的异常处理程序中调用Platform.exit()或System.exit(0)。

您可能会发现使用JavaFX Task更方便。 任务文档介绍了取消任务的方法。

但是,实际上,我不建议在示例中使用任何其他线程,而应使用时间轴,如谢尔盖(Sergey)在回答以下问题时的5秒奇迹所示: JavaFX定期后台任务

不要使用线程! 改用Timeline

Timeline clock = new Timeline(
    new KeyFrame(Duration.seconds(0), evt -> {
        LocalTime now = LocalTime.now();
        hour.set(String.format("%d", now.getHour()));            
        minute.set(String.format("%02d", now.getMinute()));            
        second.set(String.format("%d", now.getSecond()));            
    }),
    new KeyFrame(Duration.seconds(1))
);
clock.setCycleCount(Animation.INDEFINITE);
clock.play();

由于Timeline是由FX Application Thread运行的,因此您不需要通过Platform.runLater(...)任何同步。 除此之外,您可以根据需要启动和停止时间轴,并且当FX Application Thread停止时它会自动终止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM