标签上使用setText的Javafx中的性能问题



我想在OS Linux Gentoo(32bit(下编写Java FX应用程序,该应用程序通过管道测量音量,并使用.setText((方法在标签上显示值。标签上的更新速率为每20毫秒秒。含义方法标签。设置(string(每20毫秒调用。在这种情况下,JVM的CPU性能很高。大约30%!!!如果我对Java Swing Technologie做同样的事情,则CPU性能约为7%!Traget硬件是带有2 GB RAM(嵌入式系统(的E3825双核Intel原子Oracel Java版本是JRE 1.8.0.102其他Linux发行版以及Windows 10 IoT仍然存在问题。

非常奇怪的是,使用秋千的性能要好得多。我试图将文本设置在FX中的帆布上。它更好,但不多。

曾经观察到相同的行为?

thx for anwers。

附上一个示例,每10毫秒都有标签上的计数器。我有问题吗?

在这里,Java FX的代码示例:

      package appl;
  import javafx.application.Application;
  import javafx.application.Platform;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.VBox;
  import javafx.scene.text.Font;
  import javafx.scene.text.FontPosture;
  import javafx.scene.text.FontWeight;
  import javafx.stage.Stage;
  public class Main_javafx extends Application {
            public static void main(String[] args) {
                           launch(args);
            }
            @Override
            public void start(Stage primaryStage) {
                           Label label = new Label("0000000000");
                           Button button = new Button("start");
                           VBox root = new VBox();
                           root.getChildren().add(label);
                           root.getChildren().add(button);
                           label.setFont(Font.font("Arial", FontWeight.NORMAL, FontPosture.REGULAR, 100));
                           primaryStage.setScene(new Scene(root));
                           primaryStage.show();
                           button.setOnAction(e -> {
                           new Thread(() -> {
                           for (int i = 0; i < 1000000; i++) {
                               try {
                                               Thread.sleep(10);
                               }
                               catch (Exception ex) {
                               }
                               int ii = i;
                               Platform.runLater(() -> {
                                               label.setText(String.valueOf(ii));
                               });
                           }
                           }).start();
                           });
                           primaryStage.setOnCloseRequest(e -> Platform.exit());
            }

}

我没有在秋千中测试,但是使用此代码而不是您的CPU使用情况确实有100%的使用率:

public class Main_javafx extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        Label label = new Label();
        Button button = new Button("start");
        VBox root = new VBox();
        root.getChildren().add(label);
        root.getChildren().add(button);
        label.setFont(Font.font("Arial", FontWeight.NORMAL, FontPosture.REGULAR, 100));
        primaryStage.setScene(new Scene(root));
        IntegerProperty count = new SimpleIntegerProperty();
        label.textProperty().bind(count.asString());
        Timeline timeline = new Timeline(new KeyFrame(Duration.millis(10), e -> count.set(count.get() + 1)));
        timeline.setCycleCount(Animation.INDEFINITE);
        button.setOnAction(e -> timeline.play());
        primaryStage.show();
    }
}

我没有检查为什么此代码使用的CPU较少,但是我的猜测要么比手动更新和/或线程唤醒循环快于Timeline。>

最新更新