新的Thread,应用程序仍然在Stage-close之后运行

时间:2015-02-20 11:11:06

标签: multithreading javafx-8

所以我遵循了本教程:https://www.youtube.com/watch?v=gyyj57O0FVI

我在javafx8中制作了完全相同的代码。

public class CountdownController implements Initializable{

@FXML
private Label labTime;

@Override
public void initialize(URL location, ResourceBundle resources) {

    new Thread(){
        public void run(){
            while(true){
                Calendar calendar = new GregorianCalendar();
                int hour = calendar.get(Calendar.HOUR);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);
                String time = hour + ":" + minute + ":" + second;

                labTime.setText(time);
            }

        }
    }.start();
}

关闭Window后,应用程序/线程仍在系统中运行。我的猜测是因为无限循环,但是应该关闭应用程序终止线程吗?

第二件事是,当我尝试为Label设置文本时,我收到错误:

Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:204)
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:364)
    at javafx.scene.Parent$2.onProposedChange(Parent.java:364)
    at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:113)
    at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:108)
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:575)
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(LabeledSkinBase.java:204)
    at com.sun.javafx.scene.control.skin.LabelSkin.handleControlPropertyChanged(LabelSkin.java:49)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$60(BehaviorSkinBase.java:197)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$$Lambda$144/1099655841.call(Unknown Source)
    at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
    at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
    at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:103)
    at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:110)
    at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:143)
    at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:49)
    at javafx.beans.property.StringProperty.setValue(StringProperty.java:65)
    at javafx.scene.control.Labeled.setText(Labeled.java:146)
    at application.CountdownController$1.run(CountdownController.java:29)

...是的,我将阅读有关线程的更多信息,但我想知道这些问题的答案。

2 个答案:

答案 0 :(得分:3)

第一部分

线程在创建时独立于其他线程运行。你有一个新的线程,它有一个无限循环,这意味着它将永远保持运行,即使在阶段关闭后也是如此。

通常情况下,不建议使用无限循环,因为突破它是非常困难的。

建议您使用:

然后你可以调用其中一个(基于你正在使用的任何东西)

当你的舞台关闭时

你可以使用类似的东西:

stage.setOnCloseRequest(closeEvent -> {
       timertask.cancel();  
});  

JavaFX API (感谢James_D评论)

这些不需要显式取消,因为ScheduledService使用守护程序线程并且AnimationTimer在JavaFX线程上运行。

第二部分

问题的第二部分已在论坛中一次又一次地得到回答。

  

您需要在JavaFX Application线程上使用场景图元素。

由于您已创建新线程并尝试更新label(JavaFX节点),因此会抛出异常。欲了解更多信息,请访问:

JavaFX error when trying to remove shape

Why am I getting java.lang.IllegalStateException "Not on FX application thread" on JavaFX?

Javafx Not on fx application thread when using timer

答案 1 :(得分:1)

就我而言ScheduledExecutorService您无法轻易将其设为deamon而我不想使用stage.setOnCloseRequest(closeEvent -> {});

使用AnimationTimer我不能像你建议的那样做Thread.sleep(100)之间的迭代,因为“AnimationTimer在JavaFX线程上运行。”

ScheduledService让我现在很难理解......

所以,当我阅读和阅读它时,我得出结论,也许这个简单的选项将是最好的:

public class CountdownController implements Initializable{

@FXML
private Label labTime;
@FXML
private Button buttSTOP;

@Override
public void initialize(URL location, ResourceBundle resources) {
     Timer timer = new Timer(true); //set it as a deamon
     timer.schedule(new MyTimer(), 0, 1000);
}


public class MyTimer extends TimerTask{
    @Override
    public void run() {
        Calendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String time = hour + ":" + minute + ":" + second;

        Platform.runLater(() -> {
            labTime.setText(time);
        });

    }
}

感谢 James_D ItachiUchiha 。它有效,让我知道如果我失踪了!

编辑: 我还包括计算时间的代码,因为这是我最初的目标,也许有人会发现它也很有用:

public class CountdownController implements Initializable{

@FXML
private Label labTime;
@FXML
private Button buttSTOP;

private Timer timer = new Timer(true); //set it as a deamon
private int iHours = 0,
            iMinutes = 1,
            iSeconds = 10;  


public void initCountdownController(int iHours, int iMinutes, int iSeconds){
    this.iHours = iHours;
    this.iMinutes = iMinutes;
    this.iSeconds = iSeconds;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
    buttSTOP.setOnAction(e -> {
        buttSTOPAction(e);
    });
    timer.schedule(new MyTimer(), 0, 1000);
}
private void buttSTOPAction(ActionEvent e) {
    timer.cancel();
}
public class MyTimer extends TimerTask{
    @Override
    public void run() {
        String time = iHours + ":" + iMinutes + ":" + iSeconds;
        Platform.runLater(() -> {
            labTime.setText(time);
        });

        if(iSeconds < 1)
            if(iMinutes < 1)
                if(iHours < 1)
                    this.cancel();
                else{
                    iHours--;
                    iMinutes = 59;
                    iSeconds = 59;
                }
            else{
                iMinutes--;
                iSeconds = 59;
            }
        else
            iSeconds--;
    }
}