一个纯非ui非java fx线程仍在获取java.lang.illegalstateexception:不在fx应用程序线程上

4uqofj5v  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(251)

我看到了许多涉及“not on fx application thread”的错误示例,在我检查的所有示例中,都是因为有人试图从非fx线程更新ui元素。然而,在我的示例中,我从一个既没有javafx也没有ui代码的线程中得到一个错误。
我的代码的高级总结是,从控制器开始执行一个方法anal1.initializeFileList()的任务,这是一个纯计算例程,没有ui命令,如上所述。它只改变stats类中的静态可观察变量。这些变量绑定到ui元素。因此,纯非ui方法通过stats类中的变量与ui通信。我用它成功地更新了3个不同的进度条,启用和禁用了一个按钮,它的工作非常完美。我决定使用相同的方法简单地更新名为initlabel的标签的文本。我只做了两次(因此我觉得这比更新progressbar和enabling按钮要轻松得多),它生成了错误“exception in thread”thread-3“java.lang.illegalstateexception:not on fx application thread;currentthread=螺纹-3“。
考虑到我的线程是非ui和非javafx线程,你知道为什么会发生这个错误吗?如何防止错误发生?
以下是我的控制器中的相关声明:

@FXML
    private ProgressBar populateProgress;

    @FXML
    private ProgressBar sortProgress;

    @FXML
    private ProgressBar dispoProgress;

    @FXML
    private Label initLabel;

以下是在控制器中声明的非ui任务:

Task<Void> startAnal = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                anal1.intializeFileList();
                return null;
            }
        };

这是任务的开始

new Thread(startAnal).start();

这是我在控制器中的绑定。请注意,除了最后一行中与initlabel关联的绑定之外,ui的所有工作和所有绑定都在工作!

@FXML
    public void initialize() {

        // Do bindings

        startButton.disableProperty().bind(Bindings.createBooleanBinding(() -> {
            // condition for not being able to start analysis
            int bits012 = 1 | (1 <<1) | (1<<2);
            if (Stats.canStart.getValue() != bits012)
            {
                // can not start because dir's not entered or analysis running
                return true;
            }
            else {
                // valid to start and all dir's entered and analysis is not running
                return false;
            }
        },Stats.canStart));

        populateProgress.progressProperty().bind(Stats.populate);
        sortProgress.progressProperty().bind(Stats.sort);
        dispoProgress.progressProperty().bind((Stats.dispo));
        initLabel.textProperty().bind(Stats.status);
    }

下面是stats类中的一些声明:

static SimpleDoubleProperty populate =  new SimpleDoubleProperty(0.00);
    static SimpleDoubleProperty sort = new SimpleDoubleProperty(0.00);
    static SimpleDoubleProperty dispo = new SimpleDoubleProperty(0.00);

    static SimpleIntegerProperty canStart = new SimpleIntegerProperty(1<<2);
    static SimpleBooleanProperty cancel = new SimpleBooleanProperty(false);
    static SimpleStringProperty status = new SimpleStringProperty("Please Enter Directory options");

这里是生成异常的第一个(共2个)位置(在包含stats.status.set的行中):

Stats.sort.set(0.00);
        Stats.dispo.set(0.00);
        Stats.status.set("Analysis is running");
        // set not running to false
        int running = Stats.canStart.get()&(~(1<<2));
        Stats.canStart.set(running);

以下是堆栈跟踪:

Exception in thread "Thread-3" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-3
    at javafx.graphics/com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:291)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:446)
    at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:474)
    at javafx.base/com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:113)
    at javafx.base/com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:108)
    at javafx.controls/javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:272)
    at javafx.controls/javafx.scene.control.skin.LabeledSkinBase.lambda$new$11(LabeledSkinBase.java:220)
    at javafx.controls/com.sun.javafx.scene.control.LambdaMultiplePropertyChangeListenerHandler.lambda$new$1(LambdaMultiplePropertyChangeListenerHandler.java:49)
    at javafx.base/javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:86)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    at javafx.base/javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:104)
    at javafx.base/javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:111)
    at javafx.base/javafx.beans.property.StringPropertyBase$Listener.invalidated(StringPropertyBase.java:231)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:136)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    at javafx.base/javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:104)
    at javafx.base/javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:111)
    at javafx.base/javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:145)
    at dirComparer/sample.fileDB.intializeFileList(fileDB.java:371)
    at dirComparer/sample.Controller$1.call(Controller.java:299)
    at dirComparer/sample.Controller$1.call(Controller.java:296)
    at javafx.graphics/javafx.concurrent.Task$TaskCallable.call(Task.java:1425)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.lang.Thread.run(Thread.java:832)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题