JavaFX在jdk7上工作正常,但在jdk8上没有:不在FX应用程序线程上 [英] JavaFX works fine on jdk7 but not on jdk8: Not on FX application thread

查看:449
本文介绍了JavaFX在jdk7上工作正常,但在jdk8上没有:不在FX应用程序线程上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了这个问题:

I recently encountered this problem:

java.util.NoSuchElementException
线程Thread-4中的异常java.lang.IllegalStateException:不在FX上申请线程; currentThread = Thread-4

java.util.NoSuchElementException Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4

代码适用于jdk7但不适用于jdk8,代码如下:

The code works on jdk7 but not on jdk8, the code look like this:

public class myThread extends Thread {
    TextArea ta;
    String foo;
    connect(String foo, TextArea ta){
        this.ta = ta;
        this.foo = foo;
    }
    @Override
    public void run{
        Scanner scanner = new Scanner(foo);
        scanner.next();
        ta.appendText(scanner.next());
    }
}

然后我从这段代码中调用这个帖子:

Then i call this thread from this code:

    public class myApp extends Application {

        @Override
        public void start(Stage stage) {
            TextArea ta = new TextArea();
            TextField tf = new TextField
            Pane root = new Pane();
            root.getChildren().addAll(ta,tf);
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();

            tf.setOnAction((javafx.event.ActionEvent event) -> {
                String foo = tf.getText();
                tf.clear();
                new myThread(foo,ta).start();
            });
        }

        public static void main(String[] args) {
            launch(args);
        }
    }

我想做的就是更改值/属性来自其他线程的控件,就像在我的例子中一样,它会扫描下一个字符串,然后将它附加到TextArea。

All I want to do is change the values/properties of the controls from other threads which have functions just like in my example it scans for the next string then append it to the TextArea.

为什么它适用于jdk7而不是jdk8?请。解释...

Why it works on jdk7 but not on jdk8? pls. explain...

我做了一些研究和解决方案我来到这个JavaFX任务和服务但是,我没有得到它们并且没有好的在线示例。

I've done some research and the solution i came up to this JavaFX task and service but, i dont get them and there are no good examples online.

请帮忙......谢谢......

Pls help... Thanks...

推荐答案

您的代码实际上也在JDK7上被破坏了。从后台线程(即不是来自FX应用程序线程)更改UI控件的状态是非法的。与几乎所有UI工具包一样,JavaFX使用单线程模型,并且不提供UI元素上的同步。因此,如果您从FX应用程序线程以外的线程更改或查看UI控件的状态,则代码很容易出现未指定和不可预测的行为。

Your code is actually broken on JDK7 too. It is illegal to change the state of UI controls from a background thread (i.e. not from the FX Application Thread). Like almost all UI toolkits, JavaFX uses a single threaded model, and provides no synchronization on UI elements. Consequently if you change or view the state of a UI control from a thread other than the FX Application Thread, your code is prone to unspecified and unpredictable behavior.

在JavaFX 2.2中在早期(随JDK 7发布)中,运行时检查是否在正确的线程上执行代码的次数较少。因此,您不会在JDK 7中获得运行时异常,但如上所述,您的代码存在错误,并且结果不具有确定性。在JavaFX 8中,API得到了改进,因此(在大多数情况下)违反此规则会抛出 IllegalStateException 。 (这是更好的,因为你立即知道出了什么问题。)

In JavaFX 2.2 and earlier (shipping with JDK 7), there were fewer runtime checks on whether your code was being executed on the correct thread. So you don't get the runtime exception in JDK 7, but as mentioned your code is buggy and the results are not deterministic. In JavaFX 8, the API was improved so that (in most cases) violating this rule throws an IllegalStateException. (This is better as you know immediately something is wrong.)

修复是更新FX应用程序线程上的UI控件:

The fix is to update your UI controls on the FX Application Thread:

final TextArea ta = new TextArea() ;

// ...

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        ta.appendText(scanner.next());
    }
});

如果您现在只使用Java 8,则可以使用lambda表达式替换匿名内部类:

If you are now using Java 8 exclusively, you can replace the anonymous inner class with a lambda expression:

Platform.runLater(() -> ta.appendText(scanner.next()));

(并且不再需要声明 ta 作为最终,虽然仍有限制)。

(and there's no longer any need to declare ta as final, though there are still restrictions).

这篇关于JavaFX在jdk7上工作正常,但在jdk8上没有:不在FX应用程序线程上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆