2017-06-22 95 views
0

我想在os linux gentoo(32位)下编写一个Java Fx应用程序,通过管道测量卷并使用.setText()方法在标签上显示值。 标签上的更新率是每20毫秒。手段方法label.setText(string)每20毫秒调用一次。 在这种情况下,JVM的CPU性能非常高。约30%! 如果我对java swing技术做同样的事情,CPU性能约为7%! 该Traget硬件是一个E3825双核英特尔凌动与2 GB的RAM(嵌入式系统) Oracel Java版本是JRE 1.8.0.102 该问题仍然存在与其他Linux发行版,也在Windows 10物联网。JavaFx中的性能问题与标签上的setText

很奇怪的是,使用swing的表现要好得多。 我试图在fx中设置canvans上的文本。这比较好,但不是很多。

曾经观察过相同的行为?

Thx for awers。

附加一个示例,每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()); 
      } 

}

回答

1

我没有摇摆试验,但使用此代码,而不是当我没有得到有关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,但我的猜测要么结合比手动更新和/或线程唤醒睡眠周期比的速度较慢。

+0

嗨, 感谢您的意见。我在Atom目标PC上测试了你的代码,但性能差不多。用你的代码更好一点。看起来原子处理器在javafx技术上存在一些问题。 在我的台式电脑(英特尔i5)上没有性能差异。 嗯,很奇怪 – Todde

+0

@Todde我也在i5上运行。您的代码使用了大约2%的CPU,我的代码使用了大约1%。我认为在较慢的CPU上差异会放大。我建议你用代码比较和解释向Oracle或OpenJFX提交一个错误报告。 – user1803551