视图关闭时如何杀死从JavaFX控制器生成的线程

时间:2019-05-10 13:21:19

标签: java javafx

我有这个,它工作正常:

    private Timeline heartbeat;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        startHeartbeat();
    }
    private void startHeartbeat() {
        heartbeat = new Timeline(new KeyFrame(Duration.seconds(5), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                  System.out.println("beep");
            }
        }));
        heartbeat.setCycleCount(Timeline.INDEFINITE);
        heartbeat.play();
    }

但是,当视图关闭时,线程仍然存在。我在哪里放置代码以终止线程?

0 个答案:

没有答案