首页 > 解决方案 > JAVAFX 在线程中调用函数

问题描述

我目前正在尝试从我的Thread(这是一个简单的计数器)调用一个函数。没有该函数,线程运行良好,但是当我尝试调用它时,出现此错误:

Exception in thread "Thread-5" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-5
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.scene.Parent$2.onProposedChange(Parent.java:367)
at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:329)
at window.GamePane.destroyCardPane(GamePane.java:716)
at window.GamePane.timeIsUp(GamePane.java:810)
at window.GamePane$1.run(GamePane.java:74)
at java.lang.Thread.run(Unknown Source)

所以有我的线程:

public Thread getCounter() {
    if(counter == null) {
        counter = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 5; i >= 0; i--) {
                    try {
                        System.out.println(i);
                        if(i == 0) {
                            System.out.println("Time is up");
                            timeIsUp();
                            System.out.println("test");
                            break;
                        }
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        break;
                    }
                }
            }
        });
        counter.setDaemon(true);
    }
    return counter;
}

出现从 5 到 0 的数字和“时间到了”,但随后出现错误。

这是我在计数器达到 0 时尝试调用的函数:

public void timeIsUp() {
    getCounter().interrupt(); // stopping the thread? I tried without this and it's still the same error
    if (turn+1 < nbPlayers) {
        turn++;
    }  else {
        turn = 0;
    }
    getBtnDes().setDisable(false); // Disable a button
    destroyCardPane(); // remove an AnchorPane from this.children()
}

如果你们需要其他东西、类或函数来帮助我,请在评论中告诉我,我会尽快添加。

谢谢

标签: javamultithreadingfunctionjavafx

解决方案


所有 UI 操作都必须在 FX 应用程序线程上执行。您可以Platform.runLater(...)从后台线程调用并将其传递给Runnable将在 FX 应用程序线程上执行的。

这里真的不需要中断现有的线程。如果你重新排序代码以便你先睡觉,然后检查,那么线程无论如何都会退出:

public Thread getCounter() {
    if(counter == null) {
        counter = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 5; i >= 0; i--) {
                    try {
                        Thread.sleep(1000);
                        System.out.println(i);
                        if(i == 0) {
                            System.out.println("Time is up");

                            Platform.runLater(this::timeIsUp);

                            System.out.println("test");
                            break;
                        }
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        break;
                    }
                }
            }
        });
        counter.setDaemon(true);
    }
    return counter;
}

现在您可以从方法中删除中断调用timeIsUp()

public void timeIsUp() {
    if (turn+1 < nbPlayers) {
        turn++;
    }  else {
        turn = 0;
    }
    getBtnDes().setDisable(false); // Disable a button
    destroyCardPane(); // remove an AnchorPane from this.children()
}

请注意,您可以在没有线程的情况下完成所有这些操作,使用Timeline

Timeline timeline = new Timeline();
for (int i = 5 ; i >= 0 ; i=--) {
    final int count = i ;
    KeyFrame frame = new KeyFrame(Duration.seconds(5-i), e -> System.out.println(count));
    timeline.getKeyFrames().add(frame);
}
timeline.setOnFinished(e -> {
    System.out.println("Time is up");
    timeIsUp();
});
timeline.play();

这里没有阻塞(因此不需要线程),关键帧的事件处理程序和onFinished处理程序都在 FX Application Thread 上执行,因此在那里更新 UI 是安全的。


推荐阅读