有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带实时更新的java JavaFX折线图导致高CPU负载

我有一个实时更新的JavaFX折线图。每100毫秒添加一个新的数据点。该图表会导致相对较高的CPU负载。禁用带有setAnimated(false)的动画已经极大地提高了性能,但在四核2,3 GHz Intel core i7上,CPU使用率仍为130%

我的代码有什么问题吗?还是JavaFX折线图一般都很慢

package test;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.stage.Stage;

public class Main extends Application {

    static XYChart.Series quoteSeries = new XYChart.Series();

    @Override
    public void start(Stage stage) {

        final CategoryAxis xAxisLineChart = new CategoryAxis();
        final NumberAxis yAxisLineChart = new NumberAxis();
        final LineChart<String,Number> lineChart = new LineChart(xAxisLineChart,yAxisLineChart);
        lineChart.getData().add(quoteSeries);
        lineChart.setCreateSymbols(false);
        lineChart.setAnimated(false);

        Scene scene  = new Scene(lineChart,800,600);
        stage.setScene(scene);
        stage.show();
        new Thread(task).start();
    }

    // Adds each 100 ms a new data point to the chart
    Task task = new Task() {
        @Override
        protected Integer call() throws Exception {
            for (int i=0; i<1000; i++) {
                final int q = i;
                Thread.sleep(100);
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        XYChart.Data data = new XYChart.Data(""+q, q);
                        quoteSeries.getData().add(data);
                    }
                });
            }
            return null;
        }
    };

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

共 (0) 个答案