首页 文章

在JavaFX中,Platform.runLater太慢了

提问于
浏览
-1

我正在尝试在我的JavaFx应用程序中执行一个线程,我还需要更新我的listview,因为我在其中使用了Platform.runLater . 问题是它看起来太慢了,因为它会在其中跳过if状态 . listView.setItems(model.getEmailList()); 部分执行没有问题,但忽略条件,即使我打印两个值我想比较它们是不同的 . 我怎样才能改进它?因为我试图在我的JavaFX应用程序的一个线程中显示它,所以我无法在平台之外移动 if .

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 回答

  • 2

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

    emailForClient = currentOnServer

    当您使用线程时,这是一个常见问题 . 您需要修改代码的逻辑以便于并行执行 . 您可以创建一个临时变量来存储 emailForClient 并在 Platform.runLater 中使用它:

    model.reLoadData();
    final int currentEmail = emailForClient; // I'm assuming emailForClient is an int
    
    Platform.runLater(() -> {
        listView.setItems(model.getEmailList());
    
        if (currentOnServer > currentEmail) {
            new Alert(Alert.AlertType.INFORMATION, "Hai recevuto un email!").showAndWait();
        }
    });
    
    emailForClient = currentOnServer;
    

相关问题