【问题标题】:JAVAFX Call a function in a ThreadJAVAFX 在线程中调用函数
【发布时间】:2018-05-07 19:30:06
【问题描述】:

我目前正在尝试从我的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()
}

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

谢谢

【问题讨论】:

    标签: java multithreading function javafx


    【解决方案1】:

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

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

    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应用程序线程上执行,因此在那里更新UI是安全的。

    【讨论】:

    • @MA-Moustache 我肯定会推荐使用Timeline
    【解决方案2】:

    只有 JavaFX 线程可以对 JavaFX 场景图进行操作。如果您需要从另一个线程更改 JavaFX 中的某些内容,则需要使用 Platform.runLater(Runnable)

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多