Platform.runLater在JavaFX中太慢

时间:2018-12-09 13:33:01

标签: java multithreading javafx platform

我正在尝试在JavaFx应用程序中执行线程,并且我还需要更新列表视图,这就是为什么我在其中使用Platform.runLater的原因。问题在于它似乎太慢了,因为它跳过了其中的if状态。 listView.setItems(model.getEmailList());部分的执行没有问题,但是即使我打印要比较的两个值是不同的,也可以忽略条件。我该如何改善?因为无法将if移到平台之外,因为我试图在JavaFX应用程序的线程中显示它。

new Thread() {
        @Override
        public void run() {
            while (true) {
                try {
                    int currentOnServer = model.askNumbOfEmail();
                    if (emailForClient != currentOnServer) {
                        model.reLoadData();
                        Platform.runLater(() -> {
                            listView.setItems(model.getEmailList());
                            if (currentOnServer > emailForClient) {
                                new Alert(Alert.AlertType.INFORMATION, "Hai recevuto un email!").showAndWait();
                            }
                        });
                        emailForClient = currentOnServer;
                    }
                } catch (IOException ex) {
                    Thread.currentThread().interrupt();
                    return;
                } catch (ParseException ex) {
                    System.out.println("ParseException ERROR!");
                }
            }
        }
    }.start();

1 个答案:

答案 0 :(得分:2)

您的if语句不起作用,因为您要在单独的线程中更改部分条件:

df_raw = pd.DataFrame(data=df_raw) df_raw = df_raw.drop([0]) df_raw ['daily pct return']= df_raw ['daily pct return'].replace([np.inf, -np.inf],np.nan) df_raw = df_raw.replace(r'\s+', np.nan, regex=True).replace('', np.nan) df_raw.fillna(value=0, axis=1,inplace=True) df_raw.to_csv('Raw_final.csv', header=True) # Define variables for regression y = (df_raw['daily pct return']).astype(float) x1 = (df_raw['Excess daily return']).astype(float) x2 = (df_raw['Excess weekly return']).astype(float) x3 = (df_raw['Excess monthly return']).astype(float) x4 = (df_raw['Trading vol / mkt cap']).astype(float) x5 = (df_raw['Std dev']).astype(float) x6 = (df_raw['Residual risk']).astype(float) # Check shape of variables to confirm they are of the same size print(y.shape) print(x1.shape) print(x2.shape) print(x3.shape) print(x4.shape) print(x5.shape) print(x6.shape) # Perform regression result = smf.ols(formula='y ~ x1 + x2 + x3 + x4 + x5 + x6', data=df_raw).fit() print(result.params) print(result.summary())

使用线程时,这是一个常见问题。您需要修改代码的逻辑以促进并行执行。您可以创建一个临时变量来存储emailForClient = currentOnServer并在emailForClient内使用它:

Platform.runLater